jwj
jwj

Reputation: 521

Use Maven to create runnable jar with dependencies and pack that into a zip with other files

I have looked all over and have seen some similar questions and answers but nothing that seems to line up perfectly with what I am trying to achieve. I am currently able to successfully build a runnable jar with dependencies, which is great. The relevant section of my POM is this:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

                <archive>
                    <manifest>
                        <mainClass>com.main.whatever</mainClass>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>   

Now what I want to do is take the runnable jar that's created and put that into a zip file with some other files that are in a directory. I've tried adding another descriptor that points to a XML assembly file that tries to create the zip, but there is a dependency problem where it can't find the JAR file being created in the above step. I can't figure out how to specify for one to run first. I've searched high and low and just can't figure out what the best way to do it is -- there are many answers out there regarding multiple modules, multiple plugin calls, dependency sets, and so on. I'm just looking for the best practice and simplest approach.

Thanks!

Edit: So I seem to have achieved what I was trying to do by using this approach:

            <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.jasonwjones.hyperion.jessub.Jessub</mainClass>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>

                        <!-- this creates a warning during the Maven package, which I don't
                             love -->
                        <appendAssemblyId>false</appendAssemblyId>

                    </configuration>
                </execution>
                <execution>
                    <id>dist</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/dist.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

And then a simply assembly:

<assembly>
<id>dist</id>
<formats>
<format>zip</format>
</formats>
<files>
<file>
    <source>target/${project.artifactId}-${project.version}.jar</source>
  <!-- <source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source> -->
  <outputDirectory>/</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
  <directory>${project.basedir}</directory>
  <includes>
    <include>*.txt</include>
  </includes>
 <outputDirectory>/</outputDirectory> 
</fileSet>
<fileSet>
    <directory>${project.basedir}/src/main/resources</directory>
    <includes>
        <include>*</include>
    </includes>
    <outputDirectory>/</outputDirectory> 

</fileSet>

Upvotes: 1

Views: 1984

Answers (1)

Manfred Moser
Manfred Moser

Reputation: 29912

Create another module that depends on the current one that does the assembly of the executable jar and the rest of the stuff. Remember the Maven mantra .. one output artifact per module ;-)

Upvotes: 2

Related Questions