maven打包工具assembly打包本地jar的解决办法

在使用assembly打包工具打包maven项目的时候出现本地jar包无法导出的情况,通过以下配置可以解决此问题:

1.在pom.xml配置assembly工具

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tgb</groupId>
    <artifactId>mavenTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>mavenTest</name>
    <description>java service wrapper test</description>

    <properties>
        <jdk.version>1.8</jdk.version>
        <junit.version>4.11</junit.version>
        <log4j.version>1.2.16</log4j.version>
        <logback.version>1.1.7</logback.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.59</version>
        </dependency>
        <dependency>
            <groupId>org.ini4j</groupId>
            <artifactId>ini4j</artifactId>
            <version>0.5.4</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javaini</groupId>
            <artifactId>javaini</artifactId>
            <version>1.1.00</version>
            <scope>system</scope> 
            <systemPath>${project.basedir}/src/main/resources/lib/org.dtools.javaini-1.1.00.jar</systemPath>
        </dependency> 
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <!-- assembly版本 -->
                <version>2.2.1</version>
                <executions>
                    <!-- <execution> <id>make-daemon-linux-zip</id> <phase>package</phase> 
                        <goals> <goal>single</goal> </goals> <configuration> <finalName>jsw-test</finalName> 
                        <appendAssemblyId>true</appendAssemblyId> <outputDirectory>${project.build.directory}</outputDirectory> 
                        <descriptors> <descriptor>src/main/assembly/daemon-linux-zip.xml</descriptor> 
                        </descriptors> </configuration> </execution> -->
                    <execution>
                        <!-- id标识,唯一即可 -->
                        <id>make-daemon-win-zip</id>
                        <!-- 设置package阶段进行 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 只运行一次 -->
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <!-- 输出的最终名称,自动添加格式后缀(如zip),当前示例为jsw-test.zip -->
                            <finalName>jsw-test</finalName>
                            <!-- 配置是否添加id到名称中,若为true,当前示例中,则为jsw-test-zip.zip,false即不添加,只是jsw-test.zip; 
                                若同时打多个包,则可设为true,分别添加id以作区分 -->
                            <appendAssemblyId>true</appendAssemblyId>
                            <!-- 打包的输出目录,可自定义,${project.build.directory}为编译输出目录,即target目录 -->
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <descriptors>
                                <!-- 使用的描述符,按此描述进行打包,此处配置一个zip.xml表示打zip包 -->
                                <descriptor>src/main/assembly/assembly-win-zip.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                    <!-- 若要同时打多个包(如windows和linux不同系统的包),可配置多个execution,此处只打zip,因此配置一个 -->
                    <!-- <execution> id标识,唯一即可 <id>make-wrapper-win-zip</id> 设置package阶段进行 
                        <phase>package</phase> <goals> 只运行一次 <goal>single</goal> </goals> <configuration> 
                        输出的最终名称,自动添加格式后缀(如zip),当前示例为jsw-test.zip <finalName>jsw-test</finalName> 
                        配置是否添加id到名称中,若为true,当前示例中,则为jsw-test-zip.zip,false即不添加,只是jsw-test.zip; 若同时打多个包,则可设为true,分别添加id以作区分 
                        <appendAssemblyId>true</appendAssemblyId> 打包的输出目录,可自定义,${project.build.directory}为编译输出目录,即target目录 
                        <outputDirectory>${project.build.directory}</outputDirectory> <descriptors> 
                        使用的描述符,按此描述进行打包,此处配置一个zip.xml表示打zip包 <descriptor>src/main/assembly/wrapper-win-zip.xml</descriptor> 
                        </descriptors> </configuration> </execution> -->
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.配置assembly-win-zip.xml

<assembly>
    <!-- id标识,唯一即可,若pom中的appendAssemblyId设置为true,则会添加此id作为后缀 -->
    <id>daemon-win</id>
    <formats>
        <!-- 打包的格式 -->
        <format>zip</format>
    </formats>
    <!-- 打包的文件不包含项目目录,压缩包下直接是文件 -->
    <includeBaseDirectory>false</includeBaseDirectory>
    <!-- 配置依赖的输出 -->
    <dependencySets>
        <dependencySet>
            <!-- 是否把当前项目的输出jar包并使用,true则会把当前项目输出为jar包到输出目录,false不输出 -->
            <useProjectArtifact>false</useProjectArtifact>
            <scope>system</scope>
            <!-- 依赖输出目录,相对输出目录的根目录,当前示例把依赖输出到lib目录 -->
            <outputDirectory>/lib</outputDirectory>
        </dependencySet>
        <dependencySet>
            <!-- 是否把当前项目的输出jar包并使用,true则会把当前项目输出为jar包到输出目录,false不输出 -->
            <useProjectArtifact>false</useProjectArtifact>
            <scope>runtime</scope>
            <!-- 依赖输出目录,相对输出目录的根目录,当前示例把依赖输出到lib目录 -->
            <outputDirectory>/lib</outputDirectory>
        </dependencySet>
    </dependencySets>
    <!-- 文件输出 -->
    <fileSets>
        <fileSet>
            <!-- 源目录,此处是把编译出来的class文件都输出到根目录下的classes目录 -->
            <directory>${project.build.directory}/classes</directory>
            <!-- 输出目录 -->
            <outputDirectory>/classes</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>install/daemon</directory>
            <includes>
                <include>commons-daemon-1.0.15.jar</include>
            </includes>
            <outputDirectory>/lib</outputDirectory>
        </fileSet>
        <fileSet>
            <!-- 此处是把daemon文件全部输出到根目录下的install目录 -->
            <directory>install/daemon/windows</directory>
            <outputDirectory>/bin</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

注意<scope>system</scope>是实现打包本地jar包的配置

posted @ 2020-01-10 11:11  茅草屋611  阅读(4326)  评论(1编辑  收藏  举报