maven:将资源从依赖jar复制到目标文件夹

Ale*_*lex 3 java maven maven-dependency-plugin

我需要从一个依赖的jar文件到我的当前项目的输出目录的根目录的根目录复制的资源文件(menu.xml文件),在生成过程中被执行的测试之前.该文件必须可用于测试,但稍后也可用于主程序...getClassLoader().getResourceAsStream("menu.xml").

我正在尝试在这个问题中提出的解决方案,但它不起作用.

这是pom文件的样子:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>resource-dependencies</id>
        <phase>process-test-resources</phase>
        <goals>
          <goal>unpack-dependencies</goal>
        </goals>
        <configuration>
          <type>jar</type>
          <includeArtifactIds>pm1j-jar</includeArtifactIds>
          <includes>menu.xml</includes>
          <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

当我执行时,mvn clean process-test-resources我看到以下输出:

[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ pm1-config ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-dependency-plugin:2.8:unpack-dependencies (resource-dependencies) @ pm1-config ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.709 s
[INFO] Finished at: 2016-05-05T15:01:06-03:00
[INFO] Final Memory: 30M/458M
[INFO] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

文件menu.xml未复制到目标文件夹.我怎么能看到可能出错的?我尝试在调试日志级别上运行maven,并且可以看到配置已正确解析,但插件不会记录正在发生的任何其他信息.

...
[DEBUG]   (f) includeArtifactIds = pm1j-jar
[DEBUG]   (s) includes = menu.xml
...
[DEBUG]   (f) outputAbsoluteArtifactFilename = false
[DEBUG]   (s) outputDirectory = c:\Dev\workspaces\config\pm1j\pm1-config\target\classes
...
[DEBUG]   (f) project = MavenProject: com.expersoft.pm1j:pm1-config:3-SNAPSHOT @ c:\Dev\workspaces\config\pm1j\pm1-config\
pom.xml
[DEBUG] -- end configuration --
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.014 s
[INFO] Finished at: 2016-05-05T15:09:29-03:00
[INFO] Final Memory: 27M/328M
[INFO] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 11

在评论的一些帮助下,我可以找出问题所在.持有menu.xml文件的jar不在我项目的依赖项列表中.

这是对我为实现更好理解而努力实现的目标的更好解释:

我的项目在maven多模块中,我需要这个menu.xml的信息,该信息在同一个多模块的另一个项目中维护.除了这个文件之外,没有其他依赖于这个其他项目,因此,jar也没有maven依赖.

为了避免在我的项目中重复文件,到目前为止,我使用maven-resources-plugin帮助自己,该插件从另一个项目的源目录复制文件.我从来没有真正喜欢这个解决方案,因为它依赖于在文件系统上拥有这个其他项目的源代码.

  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
      <execution>
        <id>copy-resources</id>
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
          <resources>          
            <resource>
              <directory>${project.basedir}/../pm1-jJar/src/main/resources</directory>
              <includes>
                <include>menu.xml</include>
              </includes>
            </resource>
          </resources>              
        </configuration>            
      </execution>
    </executions>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

然后我发现了maven-depency-pluginunpack目标,它可以从存储库中提取下载的jar文件.这看起来像一个更清洁的解决方案.我尝试了它,它确实工作得很好.这个解决方案的问题是,unpack插件仅在测试之后的后续maven阶段起作用,并且我也使用该文件进行了一些测试.

经过一些更多的研究,我发现资源依赖插件具有unpack-dependencies目标,看起来像是完美的解决方案,但在这个阶段我完全忘记了我实际上没有对jar的依赖.


小智 10

从依赖项复制文件的一个好方法是使用 Goal unpack :

你可以在 dir/** 中提及你的文件/目录 --> 包含目录中的所有内容

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>resource-dependencies</id>
            <phase>compile</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                <artifactItem>
                    <groupId>group_id</groupId>
                    <artifactId>artifact_id</artifactId>
                    <version>1.0.0</version>
                    <type>jar</type>
                    <overWrite>true</overWrite>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </artifactItem>
                </artifactItems>
                <includes>directory_to_include/**</includes>
                <outputDirectory>${project.build.outputDirectory}</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我将目标解包附加到阶段编译中。根据您的需要进行定制。