Tharani
Tharani

Reputation: 141

how to access variable in pom.xml from external properties file?

I need to get value through a key in pom.xml from external properties files. Location of properties file is in src/main/resources/dev.properties. I have tried to solve this by using properties in maven. Please help me. pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>Simple_project</groupId>
        <artifactId>Project</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>Project Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>${Project.Name}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <outputDirectory>${path}</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
      </build>
    
    </project>
// properties File

dev.properties:

projectPath=/home/user/warfile
Project.Name = newProject

Thanks & Regards,
Tharanya B

Upvotes: 3

Views: 10402

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26492

You have to use the properties plugin:

Plugin

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

Mvn Command

mvn -DpropertiesFile=dev.properties properties:read-project-properties clean install

Alternatively

You can hardcode the file:

<file>${basedir}/dev.properties</file>

Upvotes: 5

Related Questions