【问题标题】:And in multiple file conditions does not work on Maven profile并且在多个文件条件下不适用于 Maven 配置文件
【发布时间】:2018-01-20 00:24:13
【问题描述】:

我正在使用 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 的子项目时,配置文件被激活并且插件将被执行。但配置文件在这种情况下不应该处于活动状态。我认为条件 ORed 由 maven 决定。

【问题讨论】:

    标签: maven maven-3


    【解决方案1】:

    查看v3.5.0 ActivationFile javadoc(还没有找到源)和FileProfileActivator sources,目前看来多文件是不可能的,还有this issue open

    file-activation-configuration 接受 2 个参数,一个用于现有文件,一个用于丢失文件。所以这两个参数影响同一个配置,你只能有一个这样的配置。

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

    1)ActivationFilejavadoc:

    公共类ActivationFile
      扩展对象
      实现 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2012-11-19
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多