How to place the output jar into another folder with maven?

Solution 1:

You can use the outputDirectory parameter of the maven-jar-plugin for this purpose:

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

But as cdegroot wrote, you should probably better not fight the maven way.

Solution 2:

If you want to copy the artifact into a directory outside your project, solutions might be:

  • maven-jar-plugin and configure outputDirectory
  • maven-antrun-plugin and copy task
  • copy-maven-plugin by Evgeny Goldin

Example for the copy-maven-plugin is:

<plugin>
    <groupId>com.github.goldin</groupId>
    <artifactId>copy-maven-plugin</artifactId>
    <version>0.2.5</version>
    <executions>
        <execution>
            <id>deploy-to-local-directory</id>
            <phase>install</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <skipIdentical>false</skipIdentical>
                <failIfNotFound>false</failIfNotFound>
                <resources>
                    <resource>
                        <description>Copy artifact to another directory</description>
                        <targetPath>/your/local/path</targetPath>
                        <directory>${project.build.directory}</directory>
                        <includes>
                            <include>*.jar</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Solution 3:

Another way would be maven-resources-plugin (find the current version here):

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>copy-files-on-build</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/[TO-DIR]</outputDirectory>
                <resources>
                    <resource>
                        <directory>[FROM-DIR]</directory>
                        <!--<include>*.[MIME-TYPE]</include>-->
                        <filtering>false</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Solution 4:

I would do it this way:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <configuration>
                        <target>
                            <copy file="target/${project.artifactId}-exec.jar" tofile="../../docker/${project.artifactId}.jar"/>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Solution 5:

This technique worked well for me:

http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>3.8.1</version>
                  <type>jar</type>
                  <overWrite>false</overWrite>
                  <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                  <destFileName>optional-new-name.jar</destFileName>
                </artifactItem>
              </artifactItems>
              <outputDirectory>${project.build.directory}/wars</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>