sunny.mehta
sunny.mehta

Reputation: 11

pom.xml is not able to read pom.properties file in maven

I want to move properties like version and other properties to pom.properties file. I have already tried with properties-maven-plugin but still not getting any success.Please let me know how can i achieve this.

<build>
<plugins>
                <!-- Maven clean plugin -->
                <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>pom.properties</file>
                                </files>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>....

Here is my pom.properties file data..

appserver.home=D:\apache-tomcat-7.0.64
central=http://192.168.0.110:9999/repository/internal/
snapshot=http://192.168.0.110:9999/repository/stablesnapshots/
spring.version=4.0.5.RELEASE
hibernate.version=4.3.5.Final
log4j.version=1.2.17
jdk.version=1.7
context.path=Evoke
cxf.rt.frontend.jaxrs.version=3.0.0
cxf.bundle.version=2.7.10
cxf.bundle.minimal.version=2.7.10
javax.ws.rs.api.version=2.0-m10
commons.httpclient.version=3.1
jackson.version=2.0.1
jersey.multipart.version=1.18
spring.security.version=3.2.7.RELEASE
drools.version=6.2.0.Final
itext.version=4.2.0
quartz.version=2.2.1            

Upvotes: 0

Views: 3912

Answers (1)

Daniel
Daniel

Reputation: 4221

Have you tried this?

    <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>
            <goal>write-project-properties</goal>
          </goals>
          <configuration>
            <files>
              <file>pom.properties</file>
            </files>
            <outputFile>out.properties</outputFile>
          </configuration>
        </execution>
     </executions>
   </plugin>

It should do the trick. Goals are executed in the order they are declared.

Update based on comments

The problem with having an external file to hold the versions of your dependencies is that when you install or deploy your artifact, the POM will look like this in repository, if it was possible:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
</dependency> 

When someone else tries to depend on this POM, how does he or she know what ${spring.version} resolves to? The answer is in the external file, which is embedded - if we are lucky - in the artifact (jar file). And to find the artifact in the repository, you need to know ${spring.version} so it becomes a catch-22 problem.

Now, having said this, Maven has its own mechanism for doing this:

<dependencyManagement> and <properties>:

DependencyManagement

...
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.0.5.RELEASE</version>
    </dependency>
  </dependencies>
...
<dependencies>
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <!-- No need to specify version, it is inferred from the section above -->
    </dependency>
</dependencies> 

Properties

<properties>
  <spring.version>3.0.5.RELEASE</spring.version>
</properties>
...
<dependencies>
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
<!-- spring.version can be reused, because Maven properties behave like constants -->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
</dependencies> 

The good thing is, if you you declare your versions in the <dependencyManagement> section, you can create a POM with only these type of entries in it, and have other POMs import the versions from that POM. This webpage describes how it works: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies

Since POMs always should be version controlled, you get all the added benefits from it: traceability, dependency control, portable builds, etc.

Upvotes: 1

Related Questions