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

标签 maven maven-3

我正在使用 Maven 3.5+,我读到 Maven 3.2.2+ 支持配置文件激活中的 And 条件。因此,我在配置文件的激活标记中添加了多个条件,如下所示:

 <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 进行 OR 运算的。

最佳答案

查看 v3.5.0 ActivationFile javadoc (尚未找到来源)和FileProfileActivator sources ,目前这对于多个文件似乎是不可能的,并且有 this issue open .

文件激活配置接受 2 个参数,一个用于现有文件,一个用于丢失文件。因此,这两个参数都会影响相同的配置,并且您只能有一个这样的配置。

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

1) ActivationFile javadoc:

public class ActivationFile
  extends Object
  implements Serializable, Cloneable, InputLocationTracker

This is the file specification used to activate the profile. The missing value is the location of a file that needs to exist, and if it doesn't, the profile will be activated. On the other hand, exists will test for the existence of the file and if it is there, the profile will be activated. Variable interpolation for these file specifications is limited to ${basedir}, System properties and request properties.

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;
}

关于maven - 并且在多个文件条件下不适用于 Maven 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45006255/

相关文章:

java - 如何使用 Maven 发布插件跳过集成测试

maven - 与 surefire 和 testng 并行运行测试

maven - 如何使用 Java 11 和 maven 运行 XJC?

java - Maven 中重复的直接依赖项

java - 马文 3 : Is it possible to build a JDBC-3 driver with Java 6?

Maven无法编译测试类

svn - Maven 版本 :prepare tagging fails using username and password (svn)

maven - 为什么 spark-submit 找不到 kafka 数据源,除非使用 --packages?

java - 无法找到 XML 模式命名空间的 Spring NamespaceHandler [http ://www. springframework.org/schema/data/jpa]

java - Maven项目构建失败