Java Util Packaging
需求
将Java工具类打包成jar
,方便其他项目导入依赖使用。
实现
1. 新建项目
创建Maven项目,编写pom.xml
如下:
0
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
|
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo</name>
<dependencies>
...
</dependencies>
<build>
<finalName>demo-util-1.0.0</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
2. 编写工具类
在demo/src/main/java/com/example
目录下新建工具类DemoUtil.class
。
0
1
2
3
4
5
6
7
|
package com.example;
public class DemoUtil {
public static String test() {
return "success";
}
}
|
3. 打包项目
在Maven工具中,使用assembly:assembly
打包,在demo/target
目录下将生成demo-util-1.0.0.jar
文件。
使用
在其他项目中导入demo-util-1.0.0.jar
依赖后即可使用。
0
1
2
3
4
5
6
|
import com.example.DemoUtil;
public class TestJar {
public static void main(String[] args) {
System.out.println(DemoUtil.test());
}
}
|
相关文章