复制文件夹并在 Maven 中创建一个 Jar [英] Copy a Folder and Create a Jar in Maven

查看:50
本文介绍了复制文件夹并在 Maven 中创建一个 Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a task to copy a folder and create a jar. used maven-assembly plugin to generate a jar but the problem is its creates a Folder with artificat-id + Version i dont want to create i need as is what i copied and create a 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 以及我从上述结构复制的任何文件.我需要删除文件夹 +

it ends up in creating jar with folder + and whatever the file i copied from above structure. i need to remove the Folder +

推荐答案

您可以通过不同的方式来实现,使用 maven-assembly-plugin 或使用 maven-jar-plugin:

You can do it by different ways, with the maven-assembly-plugin or with the maven-jar-plugin :

  1. 使用maven-assembly-plugin你必须在pom中声明插件的执行并像这样创建你的程序集文件,你不需要接触maven-jar-plugin(其他原因除外):
  1. With the maven-assembly-plugin you have to declare the execution of the plugin in the pom and create your assembly file like this, you don't need to touch the configuration of the maven-jar-plugin (except for other reasons) :

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.

Here I put the assembly in the folder src/assembly, and I call it jar-plus.xml.

组装 jar-plus.xml :

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

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

I put just one additionnal folder but you can configure as many as you want and set includes and excludes.

  1. 使用 maven-jar-plugin,您必须执行两次插件:
  1. With the maven-jar-plugin you have to make two executions of the plugin :

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

A first execution to generate the jar that will be the biggest one with all the resources defined with the maven-resources-plugin. And a second execution for the smallest jar in which you will describe all the excludes in order to avoid the files you don't want to have in the smallest jar (I added classifiers in my sample code bellow to make the difference between the two jars but you can avoid adding a classifier for the "normal" 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>

这篇关于复制文件夹并在 Maven 中创建一个 Jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆