Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Maven to generate output outside the project directory

Please help me I am new to Maven. I am trying to generate a target folder in different directory from the Maven project folder. According to my requirements, the generated war file should be placed in another folder like C:\naresh folder when I build my maven project.

Here my code:

<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/xsd/maven-4.0.0.xsd">
   <packaging>war</packaging>
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.test</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>

   <build>
    <directory>${project.basedir}/target</directory>
     <outputDirectory>C:\Software\${project.basedir}/target</outputDirectory>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
             <configuration>
               <warSourceDirectory>WebContent</warSourceDirectory>
             </configuration>
        </plugin>
    </plugins>

    </build>
</project>
like image 299
Naresh Avatar asked Jan 02 '13 06:01

Naresh


People also ask

How do I customize a Maven build for different environments?

A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments. Profiles are specified in pom.

Where is Maven configuration?

mvn/maven. config is located in the ${maven. projectBasedir}/. mvn/ directory; also works if in the root of a multi module build.

What is Maven configuration?

Maven has a settings file located in the Maven installation and/or user home directory that configure environmental specifics such as: HTTP proxy server. repository manager location. server authentication and passwords.

What is the use of Maven war plugin?

The WAR Plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive.


2 Answers

@khmarbaise is correct, with maven you are better off following conventions where possible - otherwise you should use a different build tool or you end up fighting it the whole way. That said, you can do the following to achieve your goal:

<build>
    <outputDirectory>${user.home}/${project.artifactId}/target</outputDirectory>
    ...
</build>

This uses the user.home system property as the root directory, and creates the output directory in a folder named after the <artifactId/> ('project'). All of the Java system properties are available in the pom. If you need this to be created elsewhere, you can just make it relative to the directory above.

Using maven-war-plugin instead:

Since your question indicates that you really just need the artifact (war file) output to a different directory, I suggest that you modify the configuration for the output directory in the maven-war-plugin. For example:

<build>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0</version>
        <configuration>
            <outputDirectory>${user.home}/${project.artifactId}</outputDirectory>
        </configuration>
        </plugin>
    </plugins>
</build>
like image 140
kldavis4 Avatar answered Oct 04 '22 10:10

kldavis4


The first thing you have to learn about Maven is: Maven has it's conventions one of those conventions is having a target folder which is located under the current project and not somewhere else. And furthermore and absolute no-go is putting some absolute folder definitions into your pom.

like image 39
khmarbaise Avatar answered Oct 04 '22 09:10

khmarbaise