【问题标题】:Sending parameters to a maven plugin向 Maven 插件发送参数
【发布时间】:2019-07-09 10:26:06
【问题描述】:

在我的 pom 文件中,我使用此配置执行构建插件。

我可以从插件代码中访问 c​​ustomProp 吗?

<execution>
...
  <configuration>
    <configOptions>
      <additional-properties>useTags=true</additional-properties>
    </configOptions>
    <customProp>custom-value</customProp>

【问题讨论】:

    标签: java maven maven-plugin spring-boot-maven-plugin


    【解决方案1】:

    假设您正在开发插件...

    是的,这是可能的。查看 Parameters 部分 Maven 的插件开发指南。

    你必须在你的 Mojo 中定义一个属性:

    @Parameter( property = "your-plugin.customProperty", defaultValue = "custom" )
    private String customProperty;
    

    【讨论】:

    • your-plugin 字符串到底是什么?从例子看来,这是我在项目中定义的目标?
    • 它没有被识别,知道为什么会这样吗?
    • "your-plugin" 是参数名称的前缀,如果你想通过命令行传递它(使用-D)
    • 我在我的项目 pom.xml 中做了&lt;configuration&gt;&lt;name&gt;value&lt;/name&gt;&lt;/configuration&gt;。在插件里我做了@Parameter(property="name"),也试过@Parameter(property="generate.name")
    • 其实我是property="${name}",可能是我的问题?
    【解决方案2】:

    如果我理解正确,当您配置 spring-boot-maven-plugin 并构建应用程序时,您可以通过 BuildProperties 对象访问有关应用程序构建的信息,例如 -

    @Autowired
    BuildProperties buildProperties;
    

    读起来像-

    // Artifact's name from the pom.xml file
    buildProperties.getName();
    // Artifact version
    buildProperties.getVersion();
    

    如果预定义的属性不够用,您可以将自己的属性从pom.xml 文件传递​​给BuildProperties

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>build-info</goal>
                </goals>
                <configuration>    
                    <additionalProperties>                    
                          <java.version>${java.version}</java.version>                    
                          <some.custom.property>some value</some.custom.property>                
                   </additionalProperties>            
                </configuration>        
         </execution>
        </executions>
    </plugin>
    

    您可以直接传递一个值或使用在pom.xml&lt;properties&gt; 部分中定义的自定义属性,然后使用${property.name} 占位符进行引用。

    您可以通过调用buildProperties.get("property.name").访问以这种方式定义的自定义属性

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    相关资源
    最近更新 更多