maven打可执行jar包及依赖jar包(二)

时间:2022-01-09 15:30:20

maven打可执行jar包,包含依赖jar包(分环境)

这篇文章打包介绍跟上一篇文章http://blog.csdn.net/u012204058/article/details/52514821
相比不同之处在于,这次打包可以分环境打包。由于各个环境的配置信息不同,所以打包的时候为了避免搞错环境,按照下面这种打包方式可以解决不同环境配置信息不同而打错包的问题,同时打出来的包的配置文件位置跟lib和bin目录同一级,也方便配置文件的修改(上一篇文章打包的配置文件位置在lib下面的一个jar包里面,一点都不方便):
打出来的包结构:
maven打可执行jar包及依赖jar包(二)
package.xml文件位置放在与pom.xml文件同等级目录下,
package.xml文件内容如下:

<assembly 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/assembly-1.0.0.xsd">
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/${zip_name}</directory>
        </fileSet>
    </fileSets>

</assembly>

2、配置pomx.ml文件插件

pom.xml文件里面加上插件配置,内容如下:

<build>
        <sourceDirectory></sourceDirectory>
        <resources>
            <!-- <resource> <directory>src/main/java</directory> <includes> <include>**/*.groovy</include> </includes> <filtering>false</filtering> </resource> -->
            <resource>
                <directory>${project.basedir}/${env.config}</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <finalName>${zip_name}</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                      <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[2.0,)</versionRange>
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                  <execution>
                    <id>make-bin</id>
                    <phase>package</phase>
                    <goals>
                      <goal>assemble</goal>
                    </goals>
                  </execution>
                </executions>
                <configuration>
                    <configurationDirectory>conf</configurationDirectory>
                    <configurationSourceDirectory>${env.config}</configurationSourceDirectory>
                    <copyConfigurationDirectory>true</copyConfigurationDirectory>
                    <assembleDirectory>${project.build.directory}/${zip_name}</assembleDirectory>
                    <binFileExtensions>
                        <unix>.sh</unix>
                    </binFileExtensions>
                    <platforms>
                        <platform>windows</platform>
                        <platform>unix</platform>
                    </platforms>
                    <repositoryName>lib</repositoryName>
                    <programs>
                        <program>
                          <mainClass>${main_class}</mainClass>
                          <name>${zip_name}</name>
                        </program>
                    </programs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <!-- not append assembly id in release file name -->
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>${project.basedir}/package.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-zip</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

同时在 <build>标签上面加上 <profiles>标签,可以按项目环境进行配置,方便各个环境发布,内容如下:

<!-- 每个profile标签表示一个环境,每个环境的配置文件对应在evn.config标签的路径下-->
<profiles>
        <profile>
            <id>dev</id>
            <properties>
               <!-- main方法主入口 --><main_class>com.leo.shell.main.Run</main_class>
              <env.config>src/main/resources/test</env.config>
              <zip_name>update-waybill-shell</zip_name>
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
               <!-- main方法主入口 --><main_class>com.leo.shell.main.Run</main_class>
              <env.config>src/main/resources/uat</env.config>
              <zip_name>update-waybill-shell</zip_name>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
              <!-- main方法主入口 --><main_class>com.leo.shell.main.Run</main_class>
              <env.config>src/main/resources/product</env.config>
              <zip_name>update-waybill-shell</zip_name>
            </properties>
        </profile>
    </profiles>

打包命令:
-p参数后面为环境名称,uat为测试环境,dev为开发环境,product为正式环境。如下面打的jar包是uat环境:
mvn clean package -Puat

对应环境的配置文件在标签里面配置好,这样打出来的包就可以防止各个环境配置信息错乱问题。