如何使用maven将输出jar放入另一个文件夹?

yel*_*lo3 46 jar maven

我想我的输出罐子,罐子,有依赖性的地方到另一个文件夹(不在target/,但在../libs/).

我怎样才能做到这一点?

Tor*_*ten 52

为此,您可以使用maven-jar-plugin 的outputDirectory参数:

<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>
Run Code Online (Sandbox Code Playgroud)

但正如cdegroot写的那样,你最好不要对抗maven方式.

  • 也许复制罐子而不是移动它们更好? (2认同)

tim*_*nen 43

如果要将工件复制到项目外的目录中,解决方案可能是:

  • maven-jar-plugin 并配置 outputDirectory
  • maven-antrun-plugin 和复制任务
  • copy-maven-plugin 作者:Evgeny Goldin

示例copy-maven-plugin是:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 这个插件不能正常工作,它抛出异常`java.lang.ClassNotFoundException:org.sonatype.aether.RepositorySystem` (9认同)

Ste*_*fan 14

另一种方式是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>
Run Code Online (Sandbox Code Playgroud)

  • @theonlygusti 非常确定这是因为它运行的阶段是“验证”。您可以将其更改为“package”或“install”,以使其在第一个构建中也能工作。 (3认同)

zjo*_*jor 8

我会这样:

        <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>
Run Code Online (Sandbox Code Playgroud)


Jas*_*ite 5

这项技术对我来说效果很好:

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>
Run Code Online (Sandbox Code Playgroud)