How do you create a standalone application with dependencies intact using Maven?

前端 未结 4 894
无人及你
无人及你 2020-12-31 12:03

I have a desktop Java application built using Maven 2 (but I could upgrade to Maven 3 if that helps) with a number of open source dependencies. I\'m now trying to package it

相关标签:
4条回答
  • 2020-12-31 12:40

    As far as I know it is possible to redistribute also LGPL libraries into your own packages as long as they are untouched. The maven assembly plugin creates a jar that contains the original jars inside a lib folder of the archive. So far you are fulfilling the LGPL.

    Maybe this question gives some more information about this topic.

    (Disclaimer: I'm not a lawyer, so please crosscheck this information ;) )

    0 讨论(0)
  • 2020-12-31 12:43

    You should check out the Maven Appassembler plugin. You can get a lot more robust package using it than rolling your own assembly.

    It generates helpful startup scripts for Unix and Windows that allow you to set predefined JAVA VM options, commandline parameters and classpath.

    It also has a concept of configuration directory where you can copy default configurations that user can later change. You can also set the configuration directory to be available on the classpath.

    Dependencies can be saved in a Maven style "repo" or you can use a flat style "lib" directory.

    You still need the assembly plugin for creating a zip or tar archive.

    Here's an example appassembler configuration:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>appassembler-maven-plugin</artifactId>
        <version>1.2.2</version>
        <configuration>
            <programs>
                <program>
                    <mainClass>com.mytools.ReportTool</mainClass>
                    <name>ReportTool</name>
                </program>
            </programs>
            <assembleDirectory>${project.build.directory}/ReportTool</assembleDirectory>
            <repositoryName>lib</repositoryName>
            <repositoryLayout>flat</repositoryLayout>
        </configuration>
        <executions>
            <execution>
                <id>assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>assemble</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    To get a zip archive I use the this assembly:

    <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>
        <formats>
            <format>zip</format>
        </formats>
        <fileSets>
            <fileSet>
                <directory>${project.build.directory}/ReportTool</directory>
                <outputDirectory>/</outputDirectory>
                <includes>
                    <include>/**</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>
    
    0 讨论(0)
  • 2020-12-31 12:58

    Try creating a custom assembly descriptor and add a dependencySet and make sure you specify, unpack as false.

    Use this as assembly descriptor,

    <?xml version="1.0" encoding="UTF-8"?>
    <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>distribution</id>
        <formats>
            <format>zip</format>
        </formats>
        <dependencySets>
            <dependencySet>
                <scope>runtime</scope>
                <outputDirectory>lib</outputDirectory>
                            <useProjectArtifact>false</useProjectArtifact>
                <unpack>false</unpack>
            </dependencySet>
        </dependencySets>
    </assembly>
    

    Store this file to say src/main/assembly/assembly.xml and update your assembly plugin configuration in pom.xml like this.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                        <configuration>
                               <descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    Here is assembly descriptor reference if you need anything more

    http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

    0 讨论(0)
  • 2020-12-31 12:58

    Add the following plugins in pom.xml. Check the value at mainClass,classpathPrefix,addClasspath tags.

    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.apache.camel.spring.Main</mainClass>
                            <classpathPrefix>lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/some-assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    Create some-assembly.xml under src/assembly as below.

    <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>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <scope>runtime</scope>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
    

    Note that useProjectArtifact flag to false, unpack flag to false. If root folder inside zip file is not required,then one can make includeBaseDirectory to false.

    This will create name-version-distribution.zip file. Inside zip file, there will be folder name-version. Inside this folder, your executable jar and lib folder containing all dependency jars will be present. Check manifest.MF file of executable jar. It contains both main class and classpath information.

    0 讨论(0)
提交回复
热议问题