【问题标题】:Copy files from one target folder to another in multimodule maven project在多模块Maven项目中将文件从一个目标文件夹复制到另一个目标文件夹
【发布时间】:2018-12-02 08:49:35
【问题描述】:

我从事多模块 Maven 项目。其中一个模块在目标文件夹中生成一些 html 文件,我需要在构建期间将它们复制到另一个模块的目标文件夹中。它们都不是 webapp。

我不知道该怎么做。我是否在 jar 中找到 html 文件,然后复制它们?有maven插件吗?

【问题讨论】:

  • 您能否提供更多关于您的构建以及生成什么样的东西以及为什么需要在不同的模块中的详细信息?

标签: java maven


【解决方案1】:

您应该依赖的 Maven 模块的唯一输出是它的工件(POM 文件、主要工件,例如 JAR、WAR、ZIP,如果您真的想要的话,它是可以通过分类器处理的附加附加工件,例如作为test-jar)。

应避免使用其他访问文件的方法,例如巧妙的相对路径欺骗,以防万一您想到这一点。

要向生成一些 HTML 文件的模块添加额外的工件,您可以使用 Maven 程序集插件的 assembly:single goal。您必须定义一个描述符来定义要包含的内容(即您的 HTML 文件)。使用appendAssemblyId(已经有true)、attachclassifier 等参数,您可以控制它成为该模块的附加附加工件,您可以通过指定分类器在另一个模块中依赖它。假设您的分类器是my-html-files,您的第二个模块可能依赖于这些 HTML 文件,如下所示:

<dependency>
  <groupId>my.group</groupId>
  <artifactId>first-module</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <classifier>my-html-files</classifier>
</dependency>

这会将 (HTML) 文件带入类路径。如果那不是您想要的地方,您可能必须先将它们解包。 unpack mojo 可能对此有用。我认为this 是一个不错的示例(请注意,此处的依赖关系表示为&lt;artifactItem/&gt;,而不是普通的&lt;dependency/&gt;)。

【讨论】:

  • 我关于使用unpack mojo 的最后一句话可以在 Arthur Noseda 的回答中看到。
【解决方案2】:

如果资源在另一个模块的 JAR 中,您可以这样使用maven-dependency-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack-some-resources</id>
            <phase>initialize</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.acme</groupId>
                        <artifactId>some-module</artifactId>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>${project.build.directory}/some-module-unpack</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

为此,com.acme:some-module 必须是您正在处理的模块的依赖项。

如果资源不在 JAR 中,您可以像这样使用普通的旧 Ant:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-somes-resources</id>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target name="copy-somes-resources">
                    <property name="dest.dir" value="${project.build.directory}/some-module-copy" />
                    <mkdir dir="${dest.dir}" />
                    <move todir="${dest.dir}">
                        <fileset dir="${project.basedir}/../some-module/target">
                            <include name="**/*.html" />
                        </fileset>
                    </move>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 虽然 Ant 建议可能适用于基本构建,但它违反了 Maven 最佳实践。 (有时你必须违背 Maven 最佳实践,我不确定这个问题是否完全值得。)此外,在执行第二个模块的Maven release 时,它会hard 崩溃。
  • 同意 Ant 路径更脆弱。然而,我不太确定你的最后论点。在多模块解决方案中,您会(以及如何做)只发布一个模块吗?
  • 人们有很多不同的发布和版本控制策略。我见过任何一种方法都在使用。
  • 哦,还有:“你会怎么做”:你只需 cd 进入相关模块并执行你的 mvn release:prepare release:perform
  • 我不知道这是可能的。我相信 maven-release-plugin 会以某种方式阻止这种情况。不会使用它,但谢谢。
猜你喜欢
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
相关资源
最近更新 更多