Maven maven-resources-plugin复制目标jar

时间:2017-02-01 16:24:55

标签: java maven maven-3

我正在尝试使用资源插件,目的是在目标目录中将jar复制到另一个目录(例如/ target / runtime)。

我可以在构建jar之前看到资源在进程开始时被复制,所以读取它似乎我需要在验证阶段运行它,即在jar构建之后。但这不起作用。其他文件被复制,但不是jar。

            <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/runtime</outputDirectory>
                        <resources>
                            <resource>
                                <filtering>true</filtering>
                                <directory>${project.basedir}/target</directory>
                                <targetPath>${project.build.directory}/target/runtime</targetPath>
                                <includes>
                                    <include>*.jar</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

2 个答案:

答案 0 :(得分:0)

`<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
          </execution>
        </executions>
        <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>
      </plugin>
    </plugins>
  </build>
  [...]
</project>`

请参考this我多次使用过这个,好像是在做这个工作。

答案 1 :(得分:0)

资源复制的插件定义中的阶段定义为validate。当此插件运行时,不会构建Jar。有关生命周期的更多信息已定义here

如果您将插件阶段更改为验证,则会成功复制文件,如下所示

Sample Project