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

And in multiple file conditions does not work on Maven profile

我正在使用 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>

我把它放在 parent 的 pom.xml 中。当 child 项目包含 index.html 但不包含 metadata.json 时,配置文件应该执行。 当我编译同时具有 index.html 和 metadata.json 的 child 项目时,配置文件已激活并且插件将执行。但在这种情况下配置文件不应处于活动状态。我认为条件由 maven 进行或运算。

查看 v3.5.0 ActivationFile javadoc (couldn't find the source yet) and FileProfileActivator sources, currently that does not seem to be possible with multiple files, and there's 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;
}