Pawan
Pawan

Reputation: 32321

Maven : Specfying directory paths to pick the file and deploy

I am using maven 2.2.1 version as a build tool for my Java Application .

With this Maven tool , i am building the war file in some directory and copying it to server (Tomcat )

This works by these below lines

<copy file="D:/MyProject/target/Test.war"
tofile="C:/Softwares/apache-tomcat-6.0.33/webapps/Test.war" />

All this works fine .

Here my question is that instead of hard coding directory the directory path , can i specifiy somewhere else ??

I have seen these project.build.directory for the src directory and project.build.outputDirectory for the target directory , can we specify this property name in the file ??

Please guide me , thanks in advance .

Upvotes: 0

Views: 9221

Answers (1)

Shane
Shane

Reputation: 4298

For the war path, you can use built in Maven properties:

${project.build.directory}/${project.build.finalName}.${project.packaging}

You want to set the deployment path as a custom Maven property. There are a few ways to do this. One is setting it directly in the pom, like this:

<properties>
  <deploy.path>C:/Softwares/apache-tomcat-6.0.33/webapps/Test.war</deploy.path>
</properties>

However, this is still hard coding the path in the pom, just in a variable.

Another way is to use the properties-maven-plugin to read in a properties file. This keeps user specific settings out of the pom, and you can keep your properties file out of source control. However, this is not the preferred Maven way of doing things, and this plugin may no longer be supported in future versions.

The Maven way to do this is to store your deploy path in your ~/.m2/settings.xml file. This property would go in a profile, which can be active by default. See this page for an explanation.

Once you have your deploy.path variable set, change your copy statement to look like this:

<copy file="${project.build.directory}/${project.build.finalName}.${project.packaging}"
    tofile="${deploy.path}" />

Edit:

On a minimal example project, the following properties are all set for me:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <id>compile</id>
        <phase>compile</phase>
        <configuration>
          <target>
            <echo message="project.build.directory: ${project.build.directory}"/>
            <echo message="project.build.finalName: ${project.build.finalName}"/>
            <echo message="project.packaging: ${project.packaging}"/>           
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

If those properties aren't set for you, can you post your pom.xml?

Upvotes: 1

Related Questions