Java中Maven项目导出jar包配置的示例代码

时间:2021-08-22 00:16:02

具体代码如下所示;

?
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
<!-- 第一种打包方式 (maven-jar-plugin), 将依赖包和配置文件放到jar包外 -->
<build>
  <sourcedirectory>src/main/java</sourcedirectory>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <!-- 将<directory>目录下的文件打包到<targetpath>下 -->
      <targetpath>${project.build.directory}</targetpath>
      <excludes>
        <exclude>**/*.java</exclude>
      </excludes>
    </resource>
  </resources>
  <testsourcedirectory>src/test/java</testsourcedirectory>
  <testresources>
    <testresource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
      <excludes>
        <exclude>**/*.java</exclude>
      </excludes>
    </testresource>
  </testresources>
  <plugins>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-compiler-plugin</artifactid>
      <version>2.3.2</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
      </configuration>
    </plugin>
    <!-- 将项目依赖包复制到<outputdirectory>指定的目录下 -->
    <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>
            <outputdirectory>
              ${project.build.directory}/lib
            </outputdirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <!-- 将项目依赖包的路径(与上一项中的<outputdirectory>对应)添加到classpath中 -->
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-jar-plugin</artifactid>
      <configuration>
        <archive>
          <manifest>
            <addclasspath>true</addclasspath>
            <classpathprefix>lib/</classpathprefix>
            <mainclass>com.ctbri.echorisk.applicationstart</mainclass>
          </manifest>
          <manifestentries>
            <class-path>./</class-path>
          </manifestentries>
        </archive>
      </configuration>
    </plugin>     
  </plugins>
</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
<!-- 第二种打包方式 (maven-shade-plugin), 将依赖包和配置文件放到jar包内 -->
<build>   
  <directory>${project.basedir}/target</directory>
  <finalname>${project.artifactid}-${project.version}</finalname>
  <plugins>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-surefire-plugin</artifactid>
      <version>2.21.0</version>
      <configuration>
        <skiptests>true</skiptests>
      </configuration>
    </plugin>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-compiler-plugin</artifactid>
      <version>2.3.2</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
        <compilerarguments>
          <extdirs>${project.basedir}/lib/</extdirs>
        </compilerarguments>
      </configuration>
    </plugin>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-shade-plugin</artifactid>
      <version>3.1.0</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <minimizejar>true</minimizejar>
            <filters>
              <filter>
                <artifact>*:*</artifact>
              </filter>
            </filters>
            <transformers>
              <!-- 往manifest文件中写入main-class是可执行包的必要条件。manifestresourcetransformer可以轻松实现。 -->
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.manifestresourcetransformer">
                <mainclass>com.ctbri.echorisk.applicationstart</mainclass>
              </transformer>
              <!-- appendingtransformer 用来处理多个jar包中存在重名的配置文件的合并,尤其是spring -->
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.appendingtransformer">
                <resource>meta-inf/spring.handlers</resource>
              </transformer>
              <transformer
                implementation="org.apache.maven.plugins.shade.resource.appendingtransformer">
                <resource>meta-inf/spring.schemas</resource>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</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
<!-- 第三种打包方式 (maven-assembly-plugin) -->
<build>
  <plugins>
    <plugin>
      <groupid>org.apache.maven.plugins</groupid>
      <artifactid>maven-surefire-plugin</artifactid>
      <version>2.21.0</version>
      <configuration>
        <skiptests>true</skiptests>
      </configuration>
    </plugin>
    <plugin>
      <artifactid>maven-compiler-plugin</artifactid>
      <version>2.3.2</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
        <compilerarguments>
          <extdirs>${project.basedir}/lib/</extdirs>
        </compilerarguments>
      </configuration>
    </plugin>
    <plugin>
      <artifactid>maven-assembly-plugin</artifactid>
      <configuration>
        <descriptorrefs>
          <descriptorref>jar-with-dependencies</descriptorref>
        </descriptorrefs>
        <archive>
          <manifest>
            <mainclass>com.ctbri.echorisk.applicationstart</mainclass>
          </manifest>
        </archive>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

总结

以上所述是小编给大家介绍的java中maven项目导出jar包配置的示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/strugglion/archive/2018/11/26/10020218.html