配置pom.xml用maven打包java工程的方法(推荐)

时间:2021-09-18 20:55:43

最近由于项目需要,研究了一下maven的打包,项目要做到

1,生成3个目录/lib,/conf,/bin目录

2,把所有的jar目录编译、拷贝到/lib目录(包括maven的jar包和lib目录下的jar,以及编译的jar包)

3,把所有的启动脚本从工程根目录拷贝到/bin目录

4,把所有的配置文件从src/main/resources拷贝到/conf

 下面是配置的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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<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>
 
  <groupId>test</groupId>
  <artifactId>test.common</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>test.common</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
  <!-- 这里省略n行 -->
  </dependencies>
 
 
  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
      <!-- 把src/main/resources目录下所有的文件拷贝到conf目录中 -->
      <resource>
        <directory>src/main/resources</directory>
        <targetPath>${project.build.directory}/conf</targetPath>
      </resource>
      <!-- 把lib目录下所有的文件拷贝到lib目录中
      (可能有些jar包没有办法在maven中找到,需要放在lib目录中) -->
      <resource>
        <directory>lib</directory>
        <targetPath>${project.build.directory}/lib</targetPath>
      </resource>
      <!-- 把放在根目录下的脚本文件.sh,.bat拷贝到bin目录中 -->
      <resource>
        <directory>.</directory>
        <includes>
          <include>**/*.sh</include>
          <include>**/*.bat</include>
        </includes>
        <targetPath>${project.build.directory}/bin</targetPath>
      </resource>
    </resources>
 
    <plugins>
      <!-- 用于编译的plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <fork>true</fork>
          <defaultLibBundleDir>lib</defaultLibBundleDir>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
          <!-- 如果配置了JAVA_HOME,下面应该可以不用配 -->
          <executable>C:\Program Files (x86)\Java\jdk1.8.0_91\bin\javac.exe</executable>
        </configuration>
      </plugin>
      
      <!-- 用于生成jar包的plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <!-- 把生成的jar包放在lib目录下(和其他所有jar包一起) -->
          <outputDirectory>${project.build.directory}/lib</outputDirectory>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
            </manifest>
          </archive>
          <excludes>
          <!-- 排除掉一些文件,不要放到jar包中,
          这里是为了排除掉src/main/resources中的文件(它们应该放到conf目录)
          这里只能指定要排除的目标文件,而不能指定源文件,虽然不够完美,但是基本能达到目的。 -->
            <exclude>*.xml</exclude>
            <exclude>*.properties</exclude>
          </excludes>
        </configuration>
      </plugin>
      
      <!-- 用于拷贝maven依赖的plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
            <!-- 把依赖的所有maven jar包拷贝到lib目录中(这样所有的jar包都在lib目录中) -->
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      
      <!-- 用于拷贝resource的plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
 
      <!-- 配置生成源代码jar的plugin -->
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <attach>true</attach>
          <encoding>UTF-8</encoding>
          <!-- 配置源代码jar文件的存放路径,和其他jar文件一起放在lib目录 -->
          <outputDirectory>${project.build.directory}/lib</outputDirectory>
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      
    </plugins>
  </build>
</project>

然后执行maven package打包。如果是eclipse,在Project Explorer-> Run As –> Maven Build… –>在Goals中输入package,点Run进行编译。

在bin目录下的启动脚本start.bat可以这么写:

?
1
2
3
4
5
6
7
8
@ECHO OFF
set CLASSPATH=%JAVA_HOME%/lib
set JAVA=%JAVA_HOME%/bin/java
 
set CLASSPATH=%CLASSPATH%;../conf
set JAVA_OPTIONS=-Djava.ext.dirs="../lib"
 
"%JAVA%" -Xms512m -Xmx1024m -classpath "%CLASSPATH%" %JAVA_OPTIONS% test.HangqingEntrance

以上:

把conf目录添加到CLASSPATH中,因为conf目录保存的是从src/main/resources拷贝来的配置文件。

设置-Djava.ext.dirs="../lib" 。因为jar包都放在lib目录。

以上就是小编为大家带来的配置pom.xml用maven打包java工程的方法(推荐)全部内容了,希望大家多多支持服务器之家~