Maven: specify the outputDirectory only for packaging a jar?

Maven 2Maven

Maven 2 Problem Overview


How can I specify the outputDirectory only for packaging a jar?

http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html this shows all parameters, but how can I set them in the commandline or pom.xml?

Maven 2 Solutions


Solution 1 - Maven 2

on command line

-DoutputDirectory=<path>

and in pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.3.1</version>
      <configuration>
        <outputDirectory>/my/path</outputDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>

Solution 2 - Maven 2

Parameter Expressions

About command line usage:

The parameter documentation specifies that the parameter is initialized to the value of the property ${project.build.directory} (which is the property referring to the target folder)

Here's what this means:

> For mojos that are intended to be > executed directly from the CLI, their > parameters usually provide a means to > be configured via system properties > instead of a <configuration/> section > in the POM. The plugin documentation > for those parameters will list an > expression that denotes the system > properties for the configuration. In > the mojo above, the parameter url is > associated with the expression > ${query.url}, meaning its value can be > specified by the system property > query.url as shown below:

mvn myquery:query -Dquery.url=http://maven.apache.org

Reference:

Configuring ${project.build.directory}

However, ${project.build.directory} is not a system property, it's a property of the Project's Build object.

You can't set maven's internal properties directly on the command line, but you can get there with a little trick by adding placeholders in your pom.xml:

<build>
    <directory>${dir}</directory>
</build>

Now, the output directory is set via the property from the command line (using -Ddir=somedirectory). Downside: now you always have to use the -Ddir parameter on the command line.

Using Profiles

But there's help here, too. Just use a profile when you want to configure the directory:

<profiles>
	<profile>
		<id>conf</id>
		<build>
			<directory>${dir}</directory>
		</build>
	</profile>
</profiles>

Now you can either do

# everything goes in someOtherDir instead of target
mvn clean install -Pconf -Ddir=someOtherDir

or plain old

# everything goes in target
mvn clean install

Configuring the Jar Plugin

Now if you just want to change the jar outputDirectory from the command line without redirecting everything from target, we'll modify the profile to configure the plugin from a command line property:

<profiles>
    <profile>
        <id>conf</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.3.1</version>
                    <configuration>
                        <outputDirectory>${dir}</outputDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

The usage is identical to above:

# everything goes in someOtherDir instead of target
mvn clean install -Pconf -Ddir=someOtherDir

Solution 3 - Maven 2

Thanks @Sean Patrick Floyd for the excellent explanation.

Instead of creating a profile and using mvn always by -P switch, I'd like to use another way that making a default value of property ${dir}.

Just define ${dir}'s default value as ${project.build.directory}

<properties>
    <dir>${project.build.directory}</dir>
</properties>

and same as @Sean Patrick Floyd, set outputDirectory.

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
            <outputDirectory>${dir}</outputDirectory>
        </configuration>
    </plugin>
</plugins>

Now you can either do

# everything goes in someOtherDir instead of target
mvn clean install -Ddir=someOtherDir

or plain old

# everything goes in target
mvn clean install

Solution 4 - Maven 2

If you wish copy dependency jars as well to a target folder, use maven-dependency-plugin.

<project>
  ...
  ...

      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>

</project>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBastlView Question on Stackoverflow
Solution 1 - Maven 2lwellerView Answer on Stackoverflow
Solution 2 - Maven 2Sean Patrick FloydView Answer on Stackoverflow
Solution 3 - Maven 2AaronView Answer on Stackoverflow
Solution 4 - Maven 2impossibleView Answer on Stackoverflow