简体   繁体   English

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

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

I work on multimodule maven project. 我从事多模块Maven项目。 One of the modules generate some html files in target folder, and I need to copy them to target folder of another module during build. 其中一个模块在目标文件夹中生成一些html文件,我需要在构建过程中将它们复制到另一个模块的目标文件夹中。 None of them is webapp. 它们都不是webapp。

I am not sure how to go about this. 我不确定该怎么做。 Do I find html files in jar, and just copy them? 我可以在jar中找到html文件,然后复制它们吗? Is there some maven plugin? 有一些Maven插件吗?

If the resources are inside the JAR of another module, you could use the maven-dependency-plugin that way: 如果资源位于另一个模块的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>

For this to work, com.acme:some-module must be a dependency of the module you're working on. 为此, com.acme:some-module必须是您正在处理的模块的依赖项。

If the resources are not inside a JAR, you can use plain old Ant like that: 如果资源不在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>

The only output from a Maven module you should be relying on are its artifacts (POM file, main artifact, eg JAR, WAR, ZIP if you really want to, and it's additional attached artifacts that can be addressed through classifiers, such as a test-jar ). 您应该依赖的Maven模块的唯一输出是它的工件(POM文件,主要工件,例如,如果您确实愿意的话,例如JAR,WAR,ZIP,它是附加的附加工件,可以通过分类器(例如test-jar来解决。 test-jar )。

Other ways to access files, such as clever relative path trickery, should be avoided, in case you were at all thinking of this. 万一您一想到这,就应该避免其他访问文件的方法,例如巧妙的相对路径欺骗。

To add an additional artifact to the module that generates some HTML files, you could use the assembly:single goal of the Maven Assembly Plugin. 要将其他工件添加到生成一些HTML文件的模块中,可以使用Maven组件插件的assembly:single目标 You'll have to define a descriptor to define what from where is to be included (ie your HTML files). 您将必须定义一个描述符来定义从何处包含内容(即HTML文件)。 With parameters such as appendAssemblyId ( true already), attach and classifier you can control that this becomes an additional attached artifact of that module, that you can depend on in the other module by specifying the classifier. 通过使用诸如appendAssemblyId (已经为true ), attachclassifier您可以控制它成为该模块的附加附加工件,您可以通过指定分类器来依赖于其他模块。 Let's say your classifier is my-html-files , your second module may depend on these HTML files as follows: 假设您的分类器是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>

This will bring the (HTML) files on the classpath. 这将使(HTML)文件进入类路径。 If that is not where you want them, you may have to unpack them first. 如果那不是您想要的地方,则可能必须先打开它们的包装。 The unpack mojo may be useful for that. unpack mojo可能对此有用。 I think this is a decent example to pick from (note that the dependency is expressed here as <artifactItem/> and not as a normal <dependency/> ). 我认为是一个不错的示例(请注意,此处的依赖关系表示为<artifactItem/>而不是常规的<dependency/> )。

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

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