简体   繁体   English

Maven MOJO-在另一个参数中使用一个参数

[英]Maven MOJO - use a parameter in another parameter

Is it possible to use a defined parameter in another parameter? 是否可以在另一个参数中使用定义的参数? For example I tried this and figured out this is not allowed: 例如,我尝试了一下,发现这是不允许的:

@Parameter(required = true)
private String semester;

@Parameter(defaultValue = "${basedir}/src/main/resources/" + semester)
private String inputFolder;

My second try was to put use the parameter as expression: 我的第二次尝试是将参数用作表达式:

@Parameter(defaultValue = "${basedir}/src/main/resources/${semester}")
private String inputFolder;

This was also a fail. 这也是一个失败。 The next iteration: I wrote the parameter as property at plugin's execution: 下一次迭代:我在插件执行时将参数写为属性:

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
    Properties props = project.getProperties();
    props.put("semester", semester);
}

This doesn't work either, because the parameters are injected before execute() is executed. 这也不起作用,因为参数是在execute()执行之前注入的。 The only solution I get to is to replace the expressions by myself: 我得到的唯一解决方案是自己替换表达式:

inputFolder = inputFolder.replace("${semester}", semester);
outputFolder = outputFolder.replace("${semester}", semester);

As you already discover: no, you can't reuse it like this. 正如您已经发现的那样:不,您不能像这样重复使用它。 The code suggest that there's a variable folder for a semester (do you really need to reuse it?). 该代码建议在一个学期中有一个可变文件夹(您是否真的需要重用它?)。 That's kind of weird: just point the inputDirectory to the correct folder, make that parameter required. inputDirectory :只需将inputDirectory指向正确的文件夹,然后将该参数inputDirectory必需即可。 Less obvious solution is to do it in code: 不太明显的解决方案是在代码中执行此操作:

File semesterFolder = new File( inputFolder, semester);

Ensure that you document the parameters very well, like how the semester parameter is used to get this folder. 确保您很好地记录了参数,例如如何使用semester参数来获取此文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM