在 maven 插件和调用 maven 项目 pom 之间共享属性
Share properties between maven plugin and the calling maven project pom
我已经创建了一个 Maven 插件 P,我想将其用作另一个 Maven 项目 A 的依赖项。我正在为来自 Maven 项目 A 的 pom 的插件 P 提供一些参数。
我想根据项目A提供的参数在插件P中设置一些属性,并希望它们在项目A的pom中被引用,我该怎么做?
我已经尝试在插件 P 中设置 MavenProject 的属性。如何在项目 A 的 pom 中引用它们?
项目 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
如果我对这个问题的理解正确,请尝试添加:
<dependencies>
<dependency>
<groupId>sample.plugin</groupId>
<artifactId>sample-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
在Plugin P
的pom.xml
文件的底部,就在</project>
结束之前
由于我对 Maven 的了解有限,我不确定这是否有效,但请告诉我。
祝你好运。
找到解决方法,如果我们在插件配置中添加${project} A作为参数,就可以给它添加属性,可以在project 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>
<project>${project}</project>
</configuration>
</execution>
</executions>
</plugin>
在Plugin one中可以使用这个Maven工程
project.getProperties.setProperty("projectProperty",propertyValue);
我已经创建了一个 Maven 插件 P,我想将其用作另一个 Maven 项目 A 的依赖项。我正在为来自 Maven 项目 A 的 pom 的插件 P 提供一些参数。
我想根据项目A提供的参数在插件P中设置一些属性,并希望它们在项目A的pom中被引用,我该怎么做?
我已经尝试在插件 P 中设置 MavenProject 的属性。如何在项目 A 的 pom 中引用它们?
项目 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
如果我对这个问题的理解正确,请尝试添加:
<dependencies>
<dependency>
<groupId>sample.plugin</groupId>
<artifactId>sample-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
在Plugin P
的pom.xml
文件的底部,就在</project>
由于我对 Maven 的了解有限,我不确定这是否有效,但请告诉我。
祝你好运。
找到解决方法,如果我们在插件配置中添加${project} A作为参数,就可以给它添加属性,可以在project 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>
<project>${project}</project>
</configuration>
</execution>
</executions>
</plugin>
在Plugin one中可以使用这个Maven工程
project.getProperties.setProperty("projectProperty",propertyValue);