我们用C++,Pascal语言的时候不存在这个问题。
如果需要引用公用的单元,只需要include/uses一下,并不需要在意公共单元的代码在哪里。
而Java项目,要求代码必须在项目内部。
导致公共单元代码被拷贝到每个类似项目的目录下。
难以统一维护,同时也生成了大量的重复代码。
所以需要:
这步比较简单,可以从一个可执行的项目开始修改。
仅需保留公共单元代码,去掉主类的代码和打包配置。
pom.xml
例子如下:
<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.0modelVersion>
<groupId>my.pub.utilsgroupId>
<artifactId>my-public-utilsartifactId>
<name>我的公共单元name>
<version>1.0.1version>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>...公共依赖1...dependency>
<dependency>...公共依赖2...dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-resources-pluginartifactId>
<version>3.3.0version>
<configuration>
<delimiters>@delimiters>
<useDefaultDelimiters>falseuseDefaultDelimiters>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.10.1version>
<configuration>
<source>8source>
<target>8target>
configuration>
plugin>
plugins>
build>
project>
用Maven打包后,
生成了文件:my-public-utils-1.0.1.jar
放置在:PublicJava
目录中。
现在需要将上面章节生成的jar包加入到其它项目的依赖中。
简单办法,可以将公共单元包加入到Maven的本地仓库中。
或者某些情况下,我们需要 手动管理这个公共单元的依赖:
PS:这个例子没有用到Spring Boot之类的框架。
需要将下面的内容加入到pom.xml
中
段。
<dependency>
<groupId>my.pub.utilsgroupId>
<artifactId>my-public-utilsartifactId>
<version>1.0.1version>
<scope>systemscope>
<systemPath>/path/my-public-utils-1.0.1.jarsystemPath>
dependency>
还需要修改pom.xml
中的maven-assembly-plugin
部分:
在
中先找到:
在同级层次中注释掉:
<configuration>
......
configuration>
再加入assembly.xml配置:
<executions>
<execution>
......
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xmldescriptor>
descriptors>
configuration>
execution>
executions>
在:项目/src/assembly/
目录下添加assembly.xml
文件,
内容如下:
<assembly>
<id>jar-with-dependenciesid>
<formats>
<format>jarformat>
formats>
<includeBaseDirectory>falseincludeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/outputDirectory>
<useProjectArtifact>trueuseProjectArtifact>
<unpack>trueunpack>
<scope>runtimescope>
dependencySet>
<dependencySet>
<outputDirectory>/outputDirectory>
<useProjectArtifact>trueuseProjectArtifact>
<unpack>trueunpack>
<scope>systemscope>
dependencySet>
dependencySets>
assembly>
完整的pom.xml
例子如下:
<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.0modelVersion>
<groupId>com.acgroupId>
<name>我的程序名称name>
<artifactId>myidartifactId>
<version>3.39.09version>
<dependencies>
<dependency>
<groupId>my.pub.utilsgroupId>
<artifactId>my-public-utilsartifactId>
<version>1.0.1version>
<scope>systemscope>
<systemPath>${pom.basedir}/../../../PublicJava/my-public-utils-1.0.1.jarsystemPath>
dependency>
<dependency>...我的依赖1...dependency>
<dependency>...我的依赖2...dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-apiartifactId>
<version>5.9.0version>
<scope>testscope>
dependency>
dependencies>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-resources-pluginartifactId>
<version>3.3.0version>
<configuration>
<delimiters>@delimiters>
<useDefaultDelimiters>falseuseDefaultDelimiters>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.10.1version>
<configuration>
<source>8source>
<target>8target>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<version>3.2.2version>
<configuration>
<archive>
<manifest>
<mainClass>com.ac.ClassNamemainClass>
manifest>
archive>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>3.4.2version>
<configuration>
<archive>
<manifest>
<mainClass>com.ac.ClassNamemainClass>
manifest>
<manifestEntries>
<Class-Path>.Class-Path>
manifestEntries>
archive>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xmldescriptor>
descriptors>
configuration>
execution>
executions>
plugin>
plugins>
build>
project>