服务器之家:专注于服务器技术及软件下载分享
分类导航

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包实例(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐