在 maven 插件和调用 maven 项目 pom 之间共享属性

我已经创建了一个 Maven 插件 P,我想将其用作另一个 Maven 项目 A 的依赖项。我正在为来自 Maven 项目 A 的 pom 的插件 P 提供一些参数。


我想根据项目 A 提供的参数在插件 P 中设置一些属性,并希望它们在项目 A 的 pom 中被引用。我该怎么做?


我已经尝试在插件 P 中为 MavenProject 设置属性。如何在项目 A 的 pom 中引用它们?


项目 A pom 片段:


<plugin>

                <groupId>sample.plugin</groupId>

                <artifactId>sample-plugin</artifactId>

                <version>1.0.0-SNAPSHOT</version>

                <executions>

                  <execution>

                    <goals>

                      <goal>testing</goal>

                    </goals>

                    <configuration>

                        <param1>value1</param1>

                        <param2>value2</param2>

                    </configuration>

                  </execution>

                </executions>

</plugin>

插件 P 代码片段


@Mojo( name = "testing")

public class TestMojo extends AbstractMojo

{


    .

    . 

    @Parameter(property = "param1")

    private String param1;


    @Parameter(property = "param2")

    private String param2;


    @Parameter(defaultValue = "${project}")

    private org.apache.maven.project.MavenProject project;


    public void execute() throws MojoExecutionException

    {

        if(param1.equalsIgnoreCase("value1")){


              project.getProperties().setProperty("PROP1","val1");


        } else{

              project.getProperties().setProperty("PROP1","val3");    

        }


        if(param2.equalsIgnoreCase("value2")){


              project.getProperties().setProperty("PROP2","val2");


        } else{


              project.getProperties().setProperty("PROP2","val3");


        }



     }

}


I expect the PROP1 and PROP2 to be used in project A


aluckdog
浏览 168回答 2
2回答

月关宝盒

如果我正确理解这个问题,请尝试添加:<dependencies>&nbsp; &nbsp;<dependency>&nbsp; &nbsp; &nbsp; <groupId>sample.plugin</groupId>&nbsp; &nbsp; &nbsp; <artifactId>sample-plugin</artifactId>&nbsp; &nbsp; &nbsp; <version>1.0.0-SNAPSHOT</version>&nbsp; &nbsp;</dependency></dependencies>Plugin P在的文件底部pom.xml,就在结束之前</project>我不确定这是否有效,因为我对 Maven 的了解有限,但请告诉我。祝你好运。

30秒到达战场

找到解决方案,如果我们在插件配置中添加${project} A作为参数,我们就可以给它添加属性,可以在project A pom中引用。前任:<plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>sample.plugin</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>sample-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.0.0-SNAPSHOT</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>testing</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <param1>value1</param1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <param2>value2</param2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <project>${project}</project>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions></plugin>在插件中可以使用这个 Maven 项目project.getProperties.setProperty("projectProperty",propertyValue);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java