【问题标题】:Copy a Folder and Create a Jar in Maven在 Maven 中复制文件夹并创建 Jar
【发布时间】:2015-05-25 02:53:48
【问题描述】:

我的任务是复制一个文件夹并创建一个 jar。使用 maven-assembly 插件生成一个 jar,但问题是它创建了一个带有 artificat-id + 版本的文件夹,我不想创建我需要的,就像我复制并创建一个 jar 一样。

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

    <id>concordian</id> 
    <formats>
        <format>jar</format>
    </formats>
    <!-- Adds the dependencies of our application -->

    <fileSets>
        <!-- Adds deploy scripts to the root directory of zip package. -->
        <fileSet>
            <directory>${project.build.directory}/concordion</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>**/**</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

它最终创建了带有文件夹 + 的 jar 以及我从上述结构复制的任何文件。我需要删除文件夹 +

【问题讨论】:

  • 你为什么要通过 maven-assmbly-plugin 而不是 maven-jar-plugin 创建一个 jar?
  • 我已经准备好创建一个 Jar 但我需要再创建一个带有一些文件的 jar。
  • with some files 是什么意思? maven-jar-plugin 的 include 选项呢?

标签: maven maven-assembly-plugin


【解决方案1】:

您可以通过不同的方式进行操作,使用maven-assembly-pluginmaven-jar-plugin

  1. 使用maven-assembly-plugin你必须在pom中声明插件的执行并像这样创建你的程序集文件,你不需要触摸maven-jar-plugin的配置(除了其他原因):

pom.xml 中:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>jar-plus</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <skipAssembly>false</skipAssembly>
                <descriptors>
                    <descriptor>src/assembly/jar-plus.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

这里我把程序集放在文件夹src/assembly,我称之为jar-plus.xml

组装jar-plus.xml:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>jar-plus</id>
    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>${project.build.outputDirectory}</directory>
        </fileSet>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>src/additionnal-folder</directory>
        </fileSet>
    </fileSets>
</assembly>

我只放了一个附加文件夹,但您可以根据需要配置任意数量并设置包含和排除。

  1. 使用maven-jar-plugin,您必须执行两次插件:

第一次执行生成 jar,该 jar 将是最大的,所有资源都使用 maven-resources-plugin 定义。对最小的 jar 进行第二次执行,您将在其中描述所有排除项,以避免在最小的 jar 中出现您不想拥有的文件(我在下面的示例代码中添加了分类器以区分两个 jar但您可以避免为“普通”jar 添加分类器)。

pom.xml 中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>

    <executions>
        <execution>
            <id>biggest-jar</id>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <classifier>biggest</classifier>
            </configuration>
        </execution>

        <execution>
            <id>normal-jar</id>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <classifier>normal</classifier>
                <excludes>
                    <exclude>**/*exclude-file*.*</exclude>
                </excludes>
            </configuration>
        </execution>
    </executions>

    <configuration>
        <includes>
            <include>**/*.*</include>
        </includes>
    </configuration>
</plugin>

【讨论】:

    猜你喜欢
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多