Maven中将项目打包成Zip,conf、lib、jar分离

1、pom文件样例如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>

    <parent>
        <groupId>com.llb</groupId>
        <artifactId>maven</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>maven</artifactId>
    <name>Maven测试</name>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk>1.8</jdk>
        <junit>4.12</junit>
        <llb.version>0.0.1-SNAPSHOT</llb.version>
        <startClass>com.llb.maven.MavenApplication</startClass>
    </properties>

    <dependencies>
        <!-- Test Framework -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.llb</groupId>
            <artifactId>core</artifactId>
            <version>${llb.version}</version>
        </dependency>
    </dependencies>

    <!-- 对应开发、测试、生产三种环境 -->
    <profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>pro</id>
            <properties>
                <env>pro</env>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 根据输入参数动态修改相关内容 -->
                <filtering>true</filtering>
                <!-- 资源根目录排除各环境的配置,防止在生成目录中多余其它目录 -->
                <excludes>
                    <exclude>dev/**</exclude>
                    <exclude>test/**</exclude>
                    <exclude>pro/**</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${env}</directory>
                <!-- 根据输入参数动态修改相关内容 -->
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <!-- 配置文件转移到config目录 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <!-- 表示把配置文件拷到和jar包同一个路径下 -->
                            <outputDirectory>
                                ${project.build.directory}/conf
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}/classes</directory>
                                    <includes>
                                        <include>**/*.properties</include>
                                        <include>**/*.xml</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- The configuration of maven-jar-plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <!-- The configuration of the plugin -->
                <configuration>
                    <!-- Configuration of the archive -->
                    <archive>
                        <!--生成的jar中,不要包含pom.xml和pom.properties这两个文件-->
                        <addMavenDescriptor>false</addMavenDescriptor>

                        <!-- Manifest specific configuration -->
                        <manifest>
                            <!--是否要把第三方jar放到manifest的classpath中-->
                            <addClasspath>true</addClasspath>
                            <!--生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/-->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--应用的main class-->
                            <mainClass>${startClass}</mainClass>
                            <!-- MANIFEST.MF文件添加SNAPSHOT依赖包的时候,不带时间戳,直接使用SNAPSHOT -->
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!--过滤掉不希望包含在jar中的文件-->
                    <excludes>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.xml</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!-- 配置依赖jar转移到lib目录 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- The configuration of maven-assembly-plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <!-- The configuration of the plugin -->
                <configuration>
                    <!-- Specifies the configuration file of the assembly plugin -->
                    <descriptors>
                        <descriptor>src/main/assembly-package.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- 不打包test代码 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
        <finalName>${project.artifactId}</finalName>
    </build>
</project>

2、 src/main/assembly-package.xml文件

<assembly>
    <id>bin</id>
    <!-- 最终打包成一个用于发布的zip文件 -->
    <formats>
        <format>zip</format>
    </formats>

    <fileSets>
        <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>

        <!-- 把项目的配置文件,打包进zip包的conf目录 -->
        <fileSet>
            <directory>${project.build.directory}/conf</directory>
            <outputDirectory>/${project.build.finalName}/conf</outputDirectory>
            <directoryMode>0755</directoryMode>
            <fileMode>0644</fileMode>
        </fileSet>

        <!-- 把项目依赖的jar包,打包进zip包的lib目录 -->
        <fileSet>
            <directory>${project.build.directory}/lib</directory>
            <outputDirectory>/${project.build.finalName}/lib</outputDirectory>
            <directoryMode>0755</directoryMode>
            <fileMode>0644</fileMode>
        </fileSet>

        <!-- 把项目编译的jar包,打包进zip包的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/${project.build.finalName}</outputDirectory>
            <includes>
                <include>${project.build.finalName}.jar</include>
            </includes>
        </fileSet>

        <!-- 把项目的脚本文件,打包进zip包的bin目录 -->
        <fileSet>
            <directory>${project.basedir}/src/bin</directory>
            <outputDirectory>/bin</outputDirectory>
            <directoryMode>0755</directoryMode>
            <fileMode>0755</fileMode>
            <lineEnding>unix</lineEnding>
            <includes>
                <include>**/*.sh</include>
                <include>*.cfg</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/bin</directory>
            <outputDirectory>/bin</outputDirectory>
            <directoryMode>0755</directoryMode>
            <fileMode>0755</fileMode>
            <lineEnding>dos</lineEnding>
            <includes>
                <include>*.bat</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>
上一篇:DOS命令学习


下一篇:一步一步跟我学习hadoop(7)----hadoop连接mysql数据库运行数据读写数据库操作