maven打包可运行的jar包(包含依赖工程)

it2022-05-05  116

<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>com.app</groupId> <artifactId>services</artifactId> <packaging>jar</packaging> <version>1.0</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.version>1.7</jdk.version> </properties> <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.sword.wechat4j</groupId> <artifactId>wechat4j</artifactId> <version>1.2.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/wechat4j-1.2.0.jar</systemPath> </dependency> <dependency> <groupId>com.app</groupId> <artifactId>base</artifactId> <version>1.0</version> <type>jar</type> <!--引用本地工程--> </dependency> </dependencies> <build> <finalName>BaiGongServer</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <includeScope>compile</includeScope> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>baigong.loganalysis.ExtractLog</mainClass> </manifest> </archive> <!--过滤掉不希望包含在jar中的文件 --> <excludes> <exclude>${project.basedir}/xml/*</exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>

内置属性(Maven预定义可以直接使用)

${basedir} 项目根目录 ${version}表示项目版本;${project.basedir}同${basedir};${project.version}表示项目版本,与${version}相同;${project.build.directory} 构建目录,缺省为target${project.build.sourceEncoding}表示主源码的编码格式;${project.build.sourceDirectory}表示主源码路径;${project.build.finalName}表示输出文件名称;${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes

 

依赖项的作用域在定义项目的依赖项的时候,我们可以通过scope来指定该依赖项的作用范围。scope的取值有compile、runtime、test、provided、system和import。compile:这是依赖项的默认作用范围,即当没有指定依赖项的scope时默认使用compile。compile范围内的依赖项在所有情况下都是有效的,包括运行、测试和编译时。runtime:表示该依赖项只有在运行时才是需要的,在编译的时候不需要。这种类型的依赖项将在运行和test的类路径下可以访问。test:表示该依赖项只对测试时有用,包括测试代码的编译和运行,对于正常的项目运行是没有影响的。provided:表示该依赖项将由JDK或者运行容器在运行时提供,也就是说由Maven提供的该依赖项我们只有在编译和测试时才会用到,而在运行时将由JDK或者运行容器提供。system:当scope为system时,表示该依赖项是我们自己提供的,不需要Maven到仓库里面去找。指定scope为system需要与另一个属性元素systemPath一起使用,它表示该依赖项在当前系统的位置,使用的是绝对路径。

 

二: 如果要引用本地工程(生产一个jar包),1: 需要将本地工程A生产jar包,并且将生产的jar添加到本地仓库;工程A pom配置

<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.app</groupId> <artifactId>base</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>base</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.5.6</version> </dependency> </dependencies> <build> <finalName>base</finalName> <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> </configuration> </plugin> </plugins> </build> </project>

 

然后右键工程A,Run As -> Maven Clean , Maven Install ,如果不出错,即可安装到Maven本地仓库中。

 

2: 在当前要引用的工程B中pom中添加dependency(如上)

用POM Editor 打开pom,

点击 "Add",

 

然后既可以添加到pom中的dependency中

 

三:将本地的jar包安装到本地库中,

例如:将IKAnalyzer-5.0.jar 注册到本地库中

在IKAnalyzer-5.0.jar所在的目录中执行cmd命令: mvn install:install-file -DgroupId=com.lxbglib -DartifactId=IKAnalyzer -Dversion=5.0 -Dpackaging=jar -Dfile=IKAnalyzer-5.0.jar

其中: com.lxbglib 是自定义的groupId然后在pom.xml 加入

<dependency>      <groupId>com.lxbglib</groupId>       <artifactId>IKAnalyzer</artifactId>      <version>5.0</version></dependency>

 

转自:http://www.cnblogs.com/benwu/articles/5182203.html 感谢原作者

转载于:https://www.cnblogs.com/panxuejun/p/6667581.html

相关资源:用Maven打成可执行jar,包含maven依赖,本地依赖的操作

最新回复(0)