maven 打包可執行 jar 檔案

不設限發表於2016-09-29
<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>
    <artifactId>dubbo-client</artifactId>
    <packaging>jar</packaging>
    <name>dubbo-client</name>
      <version>1.1</version>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>com.demo.dubbo</groupId>
        <artifactId>dubbo-parent</artifactId>
        <version>1.0</version>
        <relativePath>../dubbo-pom/pom.xml</relativePath>
    </parent>

    <dependencies>

        <dependency>
            <groupId>com.demo.dubbo</groupId>
            <artifactId>dubbo-user</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.demo.dubbo</groupId>
            <artifactId>dubbo-hello</artifactId>
            <version>0.0.1</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>dubbo-client</finalName>

        <!-- 打包資原始檔 -->
        <resources>
            <resource>
            <!-- 打包後存放的位置 -->
                <targetPath>${project.build.directory}/classes/spring</targetPath>
            <!-- 資原始檔來源位置 -->
                <directory>src/main/resources/spring</directory>
                <filtering>true</filtering>
            <!-- 包含的檔案型別 -->
                <includes>
                    <include>*.xml</include>
                </includes>
            <!-- 過濾的檔案型別 -->
                <excludes>
                </excludes>
            </resource>
            <resource>
                <targetPath>${project.build.directory}/classes</targetPath>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.xml</include>
                    <include>*.properties</include>
                </includes>
            </resource>
            <resource>
                <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
                <directory>src/main/resources/spring</directory>
                <filtering>true</filtering>
                <includes>
                    <include>spring-context.xml</include>
                </includes>
            </resource>
            <resource>
                <targetPath>${project.build.directory}/classes/META-INF</targetPath>
                <directory>src/main/resources/META-INF</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/gen</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <!-- 打包資原始檔 -->

        <sourceDirectory>src/main/java</sourceDirectory>

        <plugins>
        <!-- 編譯的 jdk 版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        <!-- 打包可執行 jar 檔案的的設定
            1.設定啟動類
            2.設定依賴包檔案的位置
            3.設定 claspath 的字首
            4.設定 Class-Path
         -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.mor.client.dubbo.main.Main</mainClass>
                            <addClasspath>true</addClasspath>
                            <useUniqueVersions>false</useUniqueVersions>
                            <!-- 依賴的jar包檔案跟當前 jar 包的位置關係 -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!-- 依賴的jar檔案存放目錄,依賴jar包跟 target同處於一個目錄下 -->
<!--                            <classpathPrefix></classpathPrefix> -->
                        </manifest>
                        <!-- Class-Path 路徑 -->
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>


        <!-- 依賴包設定 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <type>jar</type>
                            <includeTypes>jar</includeTypes>
                            <!-- 依賴 jar 包的輸出位置-->
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4</version>
                <!-- 配置檔案的編碼格式 -->
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

相關文章