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

后端 未结 5 1821
故里飘歌
故里飘歌 2020-12-01 02:04

I\'d like to place my output jar and jar-with-dependencies into another folder (not in target/ but in ../libs/).

How can I do that?

相关标签:
5条回答
  • 2020-12-01 02:21

    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>
    
    0 讨论(0)
  • 2020-12-01 02:28

    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.

    0 讨论(0)
  • 2020-12-01 02:36

    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>
    
    0 讨论(0)
  • 2020-12-01 02:42

    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>
    
    0 讨论(0)
  • 2020-12-01 02:43

    Another way would be maven-resources-plugin:

    <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
            <execution>
                <id>copy-files-on-build</id>
                <phase>validate</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>
    
    0 讨论(0)
提交回复
热议问题