并且在多个文件条件下不适用于Maven配置文件

时间:2017-07-10 07:38:09

标签: maven maven-3

我正在使用Maven 3.5+,我已经读过Maven 3.2.2+支持和条件激活配置文件。所以我在配置文件的激活标签中添加了多个条件,如下所示:

 <activation>
     <file>
         <exists>${basedir}/src/main/resources/static/index.html</exists>
          <missing>${basedir}/src/main/resources/static/app/gen-src/metadata.json</missing>
     </file>
 </activation>

我把它放在父母的pom.xml中。当子项目包含index.html但没有metadata.json时,应该执行配置文件。 当我编译同时具有index.html和metadata.json的子项目时,激活配置文件并执行插件。但在这种情况下,配置文件不应该激活。我认为条件由maven ORed。

1 个答案:

答案 0 :(得分:1)

查看v3.5.0 ActivationFile javadoc(找不到源代码)和FileProfileActivator sources,目前多个文件似乎无法实现,而且this issue open

file-activation-configuration 接受2个参数,一个用于现有参数,另一个用于丢失文件。因此,这两个参数都会影响相同的配置,您只能有一个这样的配置。

因此,如果设置了两个值,它将按此顺序查找现有文件或缺失文件,但不会同时查找这两个文件。不幸的是到目前为止我找不到解决办法......

1)ActivationFile javadoc:

  

公共类 ActivationFile   
extends Object   
实现Serializable,Cloneable,InputLocationTracker   

这是用于激活配置文件的文件规范。缺少的值是需要存在的文件的位置,如果不存在,则将激活该配置文件。另一方面,exists将测试文件是否存在,如果存在,则将激活配置文件。   这些文件规范的可变插值仅限于$ {basedir},系统属性和请求属性。

2)FileProfileActivator来源(请注意,为简洁起见,我省略了一些插值代码)

@Override
public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
    Activation activation = profile.getActivation();

    if (activation == null) {
        return false;
    }

    ActivationFile file = activation.getFile();

    if (file == null) {
        return false;
    }

    String path;
    boolean missing;

    if (StringUtils.isNotEmpty(file.getExists())) {
        path = file.getExists();
        missing = false;
    } else if (StringUtils.isNotEmpty(file.getMissing())) {
        path = file.getMissing();
        missing = true;
    } else {
        return false;
    }

    /* ===> interpolation code omitted for the sake of brevity <=== */

    // replace activation value with interpolated value
    if (missing) {
        file.setMissing(path);
    } else {
        file.setExists(path);
    }

    File f = new File(path);

    if (!f.isAbsolute()) {
        return false;
    }

    boolean fileExists = f.exists();

    return missing ? !fileExists : fileExists;
}
相关问题