亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java application maven項目如何打自定義zip包

發布時間:2021-07-12 11:40:12 來源:億速云 閱讀:153 作者:chen 欄目:編程語言

本篇內容介紹了“java application maven項目如何打自定義zip包”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

1.配置pom.xml文件,添加build節點

<build>
        <!-- 輸出的包名 -->
        <finalName>p2p</finalName>
        <sourceDirectory>src/main/java</sourceDirectory>

        <resources>
            <!-- 控制資源文件的拷貝(默認復制到classes目錄,最后打進jar包) -->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 排除外置的配置文件(運行時注釋上使IDE能讀取到配置文件;打包時放開注釋讓配置文件外置方便修改) -->
                <excludes>
                    <exclude>config.properties</exclude>
                </excludes>
            </resource>
            <!-- 配置文件外置的資源(存放到config目錄,也是classpath路徑,下面會配置) -->
           <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>config.properties</include>
                </includes>
                <targetPath>${project.build.directory}/config</targetPath>
           </resource>
       </resources>

       <plugins>
           <!-- 設置編譯版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- 打包jar文件時,配置manifest文件,加入lib包的jar依賴 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- 清單文件,設置入口類和classpath -->
                        <manifest>
                            <mainClass>com.hdwang.Application</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <!-- 給清單文件添加鍵值對,增加classpath路徑,這里將config目錄也設置為classpath路徑 -->
                        <manifestEntries>
                            <Class-Path>config/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <classesDirectory>
                    </classesDirectory>
                </configuration>
            </plugin>
            <!-- 拷貝依賴的jar包到lib目錄 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 解決資源文件的編碼問題 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- 自定義打zip包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

這個pom配置文件中注意紅色字體部分,這是實現配置文件外置的關鍵配置,思路就是配置文件不打進jar包,放置到外面,且將此文件夾設置為classpath,這樣子程序便可以通過根據classloader很方便地讀取到配置文件了。下面給出讀取配置文件的java代碼,在IDE運行時和打包后,代碼都不用修改,因為配置文件總能從classpath路徑中找到!!!

java application maven項目如何打自定義zip包

工具包的maven信息

<dependency>
    <groupId>commons-configuration</groupId>
    <artifactId>commons-configuration</artifactId>
    <version>1.10</version>
</dependency>

2.新建maven-assembly-plugin插件的配置文件assembly.xml,內容如下

<assembly>
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <!-- 使用assembly拷貝依賴包 -->
    <!--<dependencySets>-->
        <!--<dependencySet>-->
            <!--<!– 是否包含自己(將項目生成的jar包也輸出到lib目錄) –>-->
            <!--<useProjectArtifact>false</useProjectArtifact>-->
            <!--<outputDirectory>lib</outputDirectory>-->
        <!--</dependencySet>-->
    <!--</dependencySets>-->
    <fileSets>
        <!-- 從目標目錄拷貝文件去壓縮 -->
        <fileSet>
            <directory>target</directory>
            <includes>
                <include>*.jar</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target/lib</directory>
            <outputDirectory>/lib</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>target/config</directory>
            <outputDirectory>/config</outputDirectory>
        </fileSet>

        <!-- 從源目錄拷貝文件去壓縮 -->
        <fileSet>
            <directory>src/main/run</directory>
            <includes>
                <include>*.sh</include>
                <include>*.cmd</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main</directory>
            <includes>
                <include>ReadMe.txt</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

這個插件在package生命周期中運行,執行mvn package或者mvn install便可觸發此插件的執行。這里我注釋掉了拷貝依賴包的代碼,是因為在pom.xml文件中已經配置了maven-dependency-plugin執行這樣的操作,無須重復配置。fileSet可以配置需要拷貝壓縮的文件,directory路徑相對于項目根目錄,outputDirectory路徑相對于輸出目錄target,includes可以對拷貝的文件進行篩選。這里可以拷貝壓縮輸出目錄的文件,應該就是因為此插件運行在程序編譯打包之后,這樣子就達到了我們自定義打包的要求:編譯->拷貝資源文件->項目打包->拷貝依賴的jar包-> assembly進行拷貝壓縮。然后使用打出的zip包就可以去部署發布了,解壓后就能運行。

3.程序打包流程示意圖

java application maven項目如何打自定義zip包

“java application maven項目如何打自定義zip包”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

上蔡县| 磐石市| 额敏县| 玉田县| 鄯善县| 柘城县| 三门县| 新营市| 万安县| 永胜县| 张家川| 视频| 横峰县| 拉孜县| 无为县| 北海市| 鹤岗市| 铁力市| 淮北市| 奉贤区| 盖州市| 邯郸市| 阿图什市| 乌鲁木齐县| 依安县| 富裕县| 禄劝| 腾冲县| 沅陵县| 漳浦县| 历史| 连平县| 连云港市| 庄浪县| 胶州市| 科尔| 益阳市| 江阴市| 浏阳市| 平南县| 霍州市|