简体   繁体   中英

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

I work on multimodule maven project. 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. None of them is webapp.

I am not sure how to go about this. Do I find html files in jar, and just copy them? Is there some maven plugin?

If the resources are inside the JAR of another module, you could use the maven-dependency-plugin that way:

<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.

If the resources are not inside a JAR, you can use plain old Ant like that:

<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 ).

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. You'll have to define a descriptor to define what from where is to be included (ie your HTML files). 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. Let's say your classifier is my-html-files , your second module may depend on these HTML files as follows:

<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. If that is not where you want them, you may have to unpack them first. The unpack mojo may be useful for that. 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/> ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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