使用jar

时间:2015-11-11 03:04:18

标签: java eclipse maven zip pom.xml

我拥有的唯一Maven体验包括其他库,因此我需要一个非常基本的解释,说明如何使用Eclipse在Maven中实现目标。

我想定期制作我的罐子。然后我想再拿3个文件并将所有文件放在一个zip文件中。我的zip内容应如下所示:

A.jar
B.txt
C.txt
D.bat

我知道我必须在pom.xml文件中插入某些目标。我已经在Stack Overflow(即here)上看过有关类似主题的帖子。我还尝试了像here这样的Maven文档。我是否已经需要任何Maven插件或者仍然是Maven核心的东西?

但是我的技能还不足以为我的案子转移这些信息。

2 个答案:

答案 0 :(得分:10)

+ project 
    + src/main/java
    + src/main/resources
    + src/main/config
            +B.txt
            +C.txt
            +D.bat
    + src/main/assembly
            +bin.xml
    +pom.xml

bin.xml

<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>bin</id>
  <baseDirectory>/</baseDirectory>
  <formats>
    <format>zip</format>
  </formats>
   <fileSets>
    <fileSet>
      <directory>${project.basedir}/src/main/config</directory>
      <outputDirectory>/</outputDirectory> 
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>*.jar</include>
      </includes>
    </fileSet> 
  </fileSets>
</assembly>

<强> poml.xml

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/bin.xml</descriptor>
        </descriptors>
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- append to the packaging phase. -->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

输出: mvn clean package

+ artifactId-version.zip
    + B.txt
    +C.txt
    +D.txt
    +artifactId-version.jar
  

未在src / main / resources中添加B.txt,C.txt,D.bat,因为将它们放在CLASSSPATH中并不好

答案 1 :(得分:5)

当您需要创建自定义工件时,需要使用maven-assembly-plugin。我强烈建议您阅读Maven书籍的Chapter 8. Maven Assemblies,以便开始使用程序集。本章包含有关Maven程序集创建的深入说明,并且易于阅读。

基本上,在assembly descriptor的帮助下创建了一个程序集。以下程序集描述符将在zip存档的根目录中包含项目主工件。您可以在此处添加更多<file><fileSets>声明,以在归档中添加自定义文件(逻辑相同)

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>assemby-id</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
</assembly>

然后你只需要在你的POM中声明这个插件。在这种情况下,插件绑定到package阶段并使用上面的描述符进行配置。默认情况下,程序集ID将附加到存档的名称:我在此处将其删除。

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.5</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor> <!-- path to the descriptor -->
                </descriptors>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </execution>
    </executions>
</plugin>

运行mvn clean package时,您会看到将在target目录中创建一个zip存档。