7

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.

madx
  • 6,723
  • 4
  • 55
  • 59
kamaci
  • 72,915
  • 69
  • 228
  • 366

3 Answers3

7

You can use Maven resource filtering by simply adding <resources> inside <build> and turning on filtering for anything you want.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • Any hints on how to do this: https://stackoverflow.com/questions/56173541/access-file-path-from-properties-file-in-maven-while-running-junit ? – nephewtom May 17 '19 at 09:03
0

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()

Olivier Lamy
  • 2,280
  • 1
  • 14
  • 12
  • It means I should define a new variable for every new entry at properties file. For a pom file maven replaces that expressions and I want to do that job at my plugin and read from a properties file and get the value of expression or I wamt to let it maven to get me the value of an expression(without using any other plugin because I am developing mine) at a properties file? – kamaci Feb 08 '13 at 00:08
  • That was the way to get MavenProject in a maven plugin. If you want to do some filtering on files like the resources plugin do you must have a look at the component http://maven.apache.org/shared/maven-filtering/ – Olivier Lamy Feb 08 '13 at 08:11
  • is there an example how to use it? – kamaci Feb 08 '13 at 08:49
  • it writes everything into another directory is there anything to avoid it? – kamaci Feb 08 '13 at 09:50
  • you can configure http://maven.apache.org/ref/3.0.4/maven-model/apidocs/org/apache/maven/model/Resource.html with some includes and/or excludes to select which resources. – Olivier Lamy Feb 08 '13 at 11:59
  • can you check my question: http://stackoverflow.com/questions/14774667/mavenresourcesexecution-how-to-get-as-variable-instead-of-writing-to-a-file I don't want a new file at my project, I want to get the thing inside output file? – kamaci Feb 08 '13 at 14:32
  • also this can be better if you see that conversation: Custom Resource Filter Implementation http://maven.40175.n5.nabble.com/Reading-Variables-From-a-Properties-File-Without-Creating-An-Ouptut-File-td5746409.html – kamaci Feb 08 '13 at 22:23
0

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
EliuX
  • 11,389
  • 6
  • 45
  • 40