Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

properties-maven-plugin: Error loading properties-file

I want to extract all the properties from my pom.xml into a properties-file. These are the common properties like dependency-versions, plugin-versions and directories. I'm using the properties-maven-plugin, but its not working as i want it to.

The essential part of my pom.xml:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <version>1.0-alpha-1</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>read-project-properties</goal>
      </goals>
      <configuration>
       <files>
          <file>${basedir}/pom.properties</file>
        </files>
      </configuration>
    </execution>
  </executions>
</plugin>

Now when i run "mvn properties:read-project-properties" i get the following error:

[INFO] One or more required plugin parameters are invalid/missing for 'properties:read-project-properties'

[0] Inside the definition for plugin 'properties-maven-plugin' specify the following:

<configuration>
  ...
  <files>VALUE</files>
</configuration>.

The pom.properties-file is located in the same dir as the pom.xml. What can i do to let the properties-maven-plugin read my properties-file?

EDIT

I filed an issue at http://jira.codehaus.org/browse/MOJO-1523. It has been closed as "not a bug", the reason is:

It's by design. The project definition has to be self-contained, otherwise it is no longer complete if it is refered from elsewhere as part of the transitive deps.

like image 666
Wolkenarchitekt Avatar asked Apr 18 '10 23:04

Wolkenarchitekt


2 Answers

Your configuration element is defined inside an execution and thus applies to this execution only.

So either call mvn initialize (or a phase posterior to initialize) to use the configuration of your current execution binding.

Or use a global configuration:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <configuration>
     <files>
        <file>etc/config/dev.properties</file>
      </files>
    </configuration>
    ...
  </plugin>

And then call

mvn properties:read-project-properties

But that wouldn't make much sense in the particular case of this plugin (you want the properties to be available during the build) so this leaves you with the first solution.


Update: I did a test on my side and, indeed, with the following POM:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>Q2664362</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>etc/config/dev.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Running mvn test won't work: maven will try to download junit:jar:${junit.version} (i.e. it doesn't use the value of the property) and this will obviously fail.

$ mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building SO - Q2664362 - maven-properties-plugin demo
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [properties:read-project-properties {execution: default}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q2664362/src/main/resources
Downloading: http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.pom
[INFO] Unable to find resource 'junit:junit:pom:${junit.version}' in repository central (http://repo1.maven.org/maven2)
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q2664362/src/test/resources
Downloading: http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.jar
[INFO] Unable to find resource 'junit:junit:jar:${junit.version}' in repository central (http://repo1.maven.org/maven2)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
...

The odd part is that the download of the dependency occurs after properties:read-project-properties. I'm not sure but this sounds like a bug, you should open an issue.

like image 85
Pascal Thivent Avatar answered Sep 27 '22 20:09

Pascal Thivent


Try using validate phase instead of initialize for maven 3.x (link).

like image 26
user947661 Avatar answered Sep 27 '22 19:09

user947661