Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven create zip with jar and some more files

I do not understand maven. Better use ant, but... I've managed to create jar (with, or without dependencies), I've managed to copy bat runner script close to jar but now i want to create zip with this jar and this bat. So i use assembly plugin and get BUUUM!!!! CADAAAM! In my configuration it happens so, that it executes parallel to jar packaging. I wrote assembly file:

    <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>jg</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/123</outputDirectory>
            <excludes>
                <exclude>assembly/**</exclude>
                <exclude>runners/**</exclude>
            </excludes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

Then, I bound maven-assembly-plugin:

<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <inherited>false</inherited>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>by.dev.madhead.lzwj.Main</mainClass>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                    <descriptors>
                            <descriptor>src/main/resources/assembly/assembly.xml</descriptor>
                            <!-- <descriptorRef>jar-with-dependencies</descriptorRef> -->
                    </descriptors>
                </configuration>
            </execution>
        </executions>
    </plugin>

Now I get this in ./target:

  1. runner.bat
  2. jar_without_dependencies.jar (it is from maven-jar-plugin, right?)
  3. jar_without_dependencies.jar

And the third angers me. It contains:enter image description here And the 123 directory contains:enter image description here

As you see, I get jar with unpacked dependencies, EXCLUDED DIRS!!!!, and with dir 123, which is actually what I want (Oh! assembly plugin did that!!!).

I want to get jar with dependencies and correct manifest with classpath. As an option i want jar with unpacked dependencies (I know about <unpack>false</unpack> in assembly, but cannot get it work). I want to change /123 to / and get NORMAL JAR WITHOUT EXCLUDED FILES!!! I want two separate tasks to build jar and zip (is it done with profiles in maven??) As in ant, i would wrote something like this:

    <target name="jar_with_deps" depends-on="compile">
        <jar>
            here i copy classes (excluding some dirs with runner script), and build manifest
        </jar>
        <copy>
            copy bat file from src/main/resources/runner/runner.bat
        </copy>
    </target>
    <target name="zip" depends-on="jar_with_deps">
        <zip>
            Get jar from previous target, get runner.bat. Put them in zip
        </zip>
    </target>

Excuse me, if I am too expressive, but I am really angry with this implicit behavior. I am really stuck with this.

like image 551
madhead - StandWithUkraine Avatar asked Nov 26 '11 21:11

madhead - StandWithUkraine


People also ask

How do I use Maven to package a zip file?

In your maven project create a folder assembly . Add zip. xml file in the assembly folder. Add below code in zip.

Is jar and ZIP the same?

JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory. This all means you can open a jar file using the same tools you use to open a zip file.


1 Answers

Just in case it helps anyone else, I found that this was pretty easy to do, at least for my basic needs. I was already using the Maven Shade plugin to build a jar with all dependencies included:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.1</version>
  <configuration></configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
    </execution>
  </executions>
</plugin>

So when I ran mvn package, it would produce target/MyApp-version.jar, whereas I wanted a MyApp-version.zip containing MyApp-version.jar along with some other files (a README, etc.). So, I add the Assembly plugin:

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

The above block refers to assembly.xml, which configures the way the plugin works:

<?xml version="1.0" encoding="utf-8"?>
<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>release</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>target</directory>
            <includes>
                <include>MyApp-${app.version}.jar</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>CHANGES.md</source>
            <fileMode>0644</fileMode>
        </file>
        <file>
            <source>LICENSE</source>
            <fileMode>0644</fileMode>
        </file>
        <file>
            <source>README</source>
            <fileMode>0644</fileMode>
        </file>
    </files>
</assembly>

(${app.version} is defined in the pom.xml <properties> element.)

That's it, now mvn package produces both the jar and the zip.

like image 157
alexantd Avatar answered Sep 19 '22 14:09

alexantd