13

The only Maven experience I have is including other libraries so I need a very basic explanation about how I can achieve things in Maven with Eclipse.

I want to create my jar regulary. Then I want to take 3 further files and put all files together in 1 zip file. The content of my zip should look like this:

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

I know I have to insert certain goals in my pom.xml file. I already saw posts on Stack Overflow (i.e. here) regarding similar topics. I also tried the Maven documentation like here. Do I already need any Maven plugins or is still already Maven-core stuff?

But my skills are not enough yet to transfer this information for my case.

Community
  • 1
  • 1
Ernst Robert
  • 2,037
  • 4
  • 25
  • 50
  • 1
    The solution is the maven-assembly-plugin as described in the answer to the referenced (http://stackoverflow.com/questions/5717183/create-a-zip-with-all-dependencies-with-maven). What you'll have to do is include the maven-assembly-plugin into your pom.xml and create a customized assembly.xml in src/main/assemble. Generally speaking, the filesets in the assembly.xml define what will be included in the created archive. After calling mvn clean install, the new zip archive should be available in your projects target folder. Hope that helps ... – Jochen.Kohler Nov 11 '15 at 08:52
  • The default location for an assembly descriptor is `src/assembly`...See https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html – khmarbaise Nov 11 '15 at 15:05

2 Answers2

16
+ 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>

Output : mvn clean package

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

Not added B.txt, C.txt, D.bat in src/main/resources because it's not good to have them in the CLASSSPATH

question_maven_com
  • 2,457
  • 16
  • 21
8

You need to use the maven-assembly-plugin when you need to create custom artifacts. I strongly suggest that you read Chapter 8. Maven Assemblies of the Maven book to get you started with using assemblies. This chapter contains in-depth explanations on the creation of Maven assemblies, and it is easy to read.

Basically, an assembly is created with the help of an assembly descriptor. The following assembly descriptor will include the project main artifact at the root of a zip archive. You can add more <file> or <fileSets> declaration here to add your custom files in your archive (the logic is the same)

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

Then you just need to declare this plugin in your POM. In this case, the plugin is bound to the package phase and configured with the descriptor above. By default, the assembly id is appended to the name of the archive: I removed it here.

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

When running mvn clean package, you will see that a zip archive will have been created in the target directory.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I don't understand the usage of variables of: ${project.build.directory}/${project.build.finalName}.${project.packaging} In which part do you include my files A,B,C and D? – Ernst Robert Nov 12 '15 at 09:25
  • 1
    @BerndErnst the variables are here so that the part to the main artifact is not hard-coded, that's all. Concerning the files A to D, as I said in my answer, you can add `` elements for each of the file. This depends on your directory structure so I can't give a more specific answer. – Tunaki Nov 12 '15 at 09:27