一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java application maven項目打自定義zip包實例(推薦)

java application maven項目打自定義zip包實例(推薦)

2020-10-07 22:39JAVA之家 Java教程

下面小編就為大家帶來一篇java application maven項目打自定義zip包實例(推薦)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<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信息

?
1
2
3
4
5
<dependency>
  <groupId>commons-configuration</groupId>
  <artifactId>commons-configuration</artifactId>
  <version>1.10</version>
</dependency>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<assembly>
  <id>bin</id>
  <formats>
    <format>zip</format>
  </formats>
  <!-- 使用assembly拷貝依賴包 -->
  <!--<dependencySets>-->
    <!--<dependencySet>-->
      <!--&lt;!&ndash; 是否包含自己(將項目生成的jar包也輸出到lib目錄) &ndash;&gt;-->
      <!--<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包實例(推薦)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日日骑夜夜骑 | 俄罗斯处女摘花 | 免费370理论片中文字幕 | 国产私拍精品88福利视频 | 亚洲精品一区二区三区在线看 | 午夜国产精品影院在线观看 | 忘忧草秋观看未满十八 | 疯狂激吻添下边小说 | ak福利影院 | 国产视频一区二区 | 2012在线观看免费视频大全 | 日本色女 | 双性少爷受糙汉攻h | 久久综合久综合久久鬼色 | 湖南美女被黑人4p到惨叫 | 九色PORNY丨视频入口 | 国产欧美日韩专区毛茸茸 | 性色老女人 | 6080欧美一区二区三区四区 | 美女被草哭 | poren黑人 | 黄瓜污视频 | 秋霞在线观看成人高清视频51 | 黑人巨| 亚洲AV无码A片在线观看蜜桃 | 欧美精品综合一区二区三区 | 国产大片免费在线观看 | 亚洲精品国产自在现线最新 | 日韩 国产 欧美 | 精品国产欧美一区二区 | 欧美精品一区二区三区免费播放 | 91麻豆国产福利在线观看 | 好男人资源免费播放 | caoporn超碰| 国产精品国语自产拍在线观看 | 男人的天堂欧美 | 激情亚洲 | 精品国产欧美一区二区 | 日韩网站在线观看 | 精品久久成人 | 亚洲国产欧美久久香综合 |