Skip to content

Instantly share code, notes, and snippets.

@zhiguangwang
Created November 10, 2015 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhiguangwang/5b57305bf4b1aa576795 to your computer and use it in GitHub Desktop.
Save zhiguangwang/5b57305bf4b1aa576795 to your computer and use it in GitHub Desktop.
Packages maven project as runnable jar and library jars.

##Desired file hierarchy

Run mvn clean package expects the following layouts:

target folder layout

target
├── ${project.artifactId}-${project.version}
│   ├── lib
│   │   ├── jersey-bean-validation-2.22.1.jar
│   │   ├── jersey-client-2.22.1.jar
│   │   ├── jersey-common-2.22.1.jar
│   │   ├── jersey-container-grizzly2-http-2.22.1.jar
│   │   ├── jersey-container-servlet-2.22.1.jar
│   │   ├── jersey-container-servlet-core-2.22.1.jar
│   │   ├── jersey-entity-filtering-2.22.1.jar
│   │   ├── jersey-guava-2.22.1.jar
│   │   ├── jersey-media-jaxb-2.22.1.jar
│   │   ├── jersey-media-json-jackson-2.22.1.jar
│   │   ├── jersey-server-2.22.1.jar
│   └── ${project.artifactId}.jar
├── ${project.artifactId}-${project.version}.tar.gz
├── ${project.artifactId}-${project.version}.zip
├── ${project.artifactId}.jar

bin files layout

${project.artifactId}-${project.version}.(bin|tar.gz)
├── lib
│   ├── jersey-bean-validation-2.22.1.jar
│   ├── jersey-client-2.22.1.jar
│   ├── jersey-common-2.22.1.jar
│   ├── jersey-container-grizzly2-http-2.22.1.jar
│   ├── jersey-container-servlet-2.22.1.jar
│   ├── jersey-container-servlet-core-2.22.1.jar
│   ├── jersey-entity-filtering-2.22.1.jar
│   ├── jersey-guava-2.22.1.jar
│   ├── jersey-media-jaxb-2.22.1.jar
│   ├── jersey-media-json-jackson-2.22.1.jar
│   ├── jersey-server-2.22.1.jar
└── ${project.artifactId}.jar

Maven configurations

pom.xml

<project>
    ...
    <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <finalName>${project.artifactId}</finalName>
                    <archive>
                        <manifest>
                            <mainClass>com.unigames.siege.Main</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>exploded-dir</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/assembly/exploded-dir.xml</descriptor>
                            </descriptors>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                    </execution>
                    <execution>
                        <id>bin</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/assembly/bin.xml</descriptor>
                            </descriptors>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    ...
</project>

exploded-dir.xml

<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>exploded-dir</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>.</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
            <excludes>
                <exclude>${artifact}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

bin.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>bin</id>
    <formats>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>.</outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/${project.build.finalName}</directory>
            <outputDirectory>.</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment