Reputation: 75257
I am developing a custom maven plugin. When I write:
${project.version}
into my pom file I can get its value however is there a way if I write into a properties file:
project.version = ${project.version}
that will set the project.version value correctly, how can I implement it at my Java code?
PS: I don't use annotations at my Mojo and I don't want to use variables at my Java code because user should define as my variables as at a properties file and I can not change my core Java code in order to changed things.
Upvotes: 7
Views: 7566
Reputation: 12685
You may read and save any property in your pom.xml
as i do in this function:
/**
* Save a property in a pom.xml
*
* @param propertyName Name of the property
* @param value New value for the property
*/
public static void saveProjectProperty(String propertyName, String value)
{
Model model = null;
FileReader reader = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
try
{
reader = new FileReader("pom.xml");
model = mavenreader.read(reader);
MavenProject project = new MavenProject(model);
while (project.getParent() != null)
{
project = project.getParent();
}
project.getProperties().put(propertyName, value);
try (FileWriter fileWriter = new FileWriter("pom.xml"))
{
project.writeModel(fileWriter);
}
}
catch (IOException ex)
{
LOG.severe("Error saving pom.xml");
}
catch (XmlPullParserException ex)
{
LOG.warning("Error reading pom.xml");
}
}
To be able to use the clases not native in the JVM you should add these dependencies:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0</version>
</dependency
Upvotes: 0
Reputation: 33511
You can use Maven resource filtering by simply adding <resources>
inside <build>
and turning on filtering for anything you want.
Upvotes: 7
Reputation: 2310
within your mojo if using annotations see http://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html use
@Component
protected MavenProject project;
with doclet
/**
* The Maven project to act upon.
*
* @parameter expression="${project}"
* @required
*/
private MavenProject project;
then project.getVersion()
Upvotes: 0