1. 程式人生 > >基於maven進行編譯打包的通用例子

基於maven進行編譯打包的通用例子

1、相關外掛

  • maven-jar-plugin

    單獨將工程單獨打包成單獨的 jar 利於維護,根據需求將相應的配置檔案和資源納入或排除打包範圍,便於資源維護

  • maven-dependency-plugin

    單獨下載依賴包便於釋出維護

  • maven-assembly-plugin

    歸檔打包釋出

2、打包目錄規劃

-app
    |-bin       運維指令碼存放目錄
    |-config    配置等資原始檔存放目錄
    |-lib       jar包存放目錄

3、pom.xml外掛配置

    <plugins
>
<!-- 如果是spring boot專案的話 去除掉 spring boot 打包整合的外掛 不完全打成一個獨立的jar 便於對資原始檔的管理和維護--> <!--<plugin>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-maven-plugin</artifactId>--> <!--</plugin>-->
<plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>make</id> <phase>package</phase> <goals> <goal>single</goal> </goals
>
<configuration> <finalName>appDemo</finalName> <descriptors> <descriptor>src/assembly/app.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <!-- need at least 2.6 for useBaseVersion--> <version>2.8</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> </execution> </executions> <configuration> <includeTypes>jar</includeTypes> <overWriteSnapshots>true</overWriteSnapshots> <type>jar</type> <outputDirectory>${project.build.directory}/lib</outputDirectory> <!-- useBaseVersion=false makes the jar names match those that maven-jar-plugin puts in the manifest classpath --> <useBaseVersion>false</useBaseVersion> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <classesDirectory>${project.build.outputDirectory}</classesDirectory> <excludes> <exclude>**/*.jar</exclude> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> <exclude>**/*.js</exclude> <exclude>**/*.html</exclude> </excludes> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix></classpathPrefix> <mainClass>cn.coremail.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins>
  • app.xml
<assembly>
    <id>app</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeSiteDirectory>false</includeSiteDirectory>
    <fileSets>
        <fileSet>
            <includes>
                <include>README.md</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/bin</directory>
            <outputDirectory>/bin</outputDirectory>
            <includes>
                <include>*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>lib/**/*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <outputDirectory>/lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>target/classes</directory>
            <outputDirectory>/config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
                <include>static/**/*.*</include>
                <include>templates/**/*.*</include>
            </includes>
        </fileSet>

    </fileSets>

</assembly>

4、執行打包命令,即可打包成功準備釋出了

mvn package -PmakeAll -Dmaven.test.skip=true