简体   繁体   中英

Reading properties file values into pom.xml

I've tried and follow this solution How to read an external properties file in Maven but with no luck.

I would like to read an absolute path(/home/tomcat/lib) from a properties file(which is in the same location as pom.xml) and set the value in the pom.xml

project.properties file contains:

myTomCat.lib.location=/home/tomcat/lib

pom.xml configuration contains:

<properties>
    <envTomcatLib>${myTomCat.lib.location}</envTomcatLib>
</properties>

<dependencies>
<dependency>
           <artifactId>MyJar</artifactId>
       <groupId>MyJar</groupId>
       <scope>system</scope>
       <version>1.0</version>
       <systemPath>/${envTomcatLib}/MyJar.jar</systemPath>
 </dependency>
</dependencies>

  <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}/project.properties</file>
          </files>
        </configuration>
      </execution>
    </executions>
  </plugin>  

The problem is either at eclipse compile time or running "mvn install" that the placeholder ${envTomcatLib}/MyJar.jar, can't get resolved to /home/tomcat/lib/MyJar.jar and remains ${envTomcatLib}/MyJar.jar.

Can someone please assist?

Thanks

Values in the <properties> section are assigned when the POM is initially loaded. The properties-maven-plugin only affects plugin executions that come after the point where the properties were loaded. More detail in a similar answer I provided.

BTW, the Maven Reference Book has this to say about system scope: "Note that this scope is not recommended (you should always try to reference dependencies in a public or custom Maven repository)."

You may need to remove "/" before /${envTomcatLib}/MyJar.jar or give property value as home/tomcat/lib instead of /home/tomcat/lib . It may be the reason for not able to resolve your location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM