繁体   English   中英

将文件从一个目标文件夹复制到多模块Maven项目中的另一个文件夹

[英]Copy files from one target folder to another in multimodule maven project

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

我不确定该怎么做。 我可以在jar中找到html文件,然后复制它们吗? 有一些Maven插件吗?

如果资源位于另一个模块的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>

您应该依赖的Maven模块的唯一输出是它的工件(POM文件,主要工件,例如,如果您确实愿意的话,例如JAR,WAR,ZIP,它是附加的附加工件,可以通过分类器(例如test-jar来解决。 test-jar )。

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

要将其他工件添加到生成一些HTML文件的模块中,可以使用Maven组件插件的assembly:single目标 您将必须定义一个描述符来定义从何处包含内容(即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可能对此有用。 我认为是一个不错的示例(请注意,此处的依赖关系表示为<artifactItem/>而不是常规的<dependency/> )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM