簡體   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