具有依赖关系的 Maven jar?
Maven jar with dependencies?
我想构建项目,以便最终 jar 将所有依赖项包含在一个 jar 文件中
(如果不可能,将 类 从依赖项包含到 jar 文件中)
我关注了线程 Including dependencies in a jar with Maven 但它包括在内
我什至没有在我的 pom.xml 中提到的依赖项。这是我的 POM,它只有两个依赖项。
我希望在构建 final 时,它包含 pom 中提到的特定依赖项(类 或 jar 形式)
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.myProject</groupId>
<artifactId>utils</artifactId>
<version>1</version>
</dependency>
一种方法是使用 maven assembly 插件,pom.xml 配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.taobao.top.appstore.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
在这里你可以找到一个例子pom.xml如何用maven-shade-plugin
解决它
当您 运行 mvn package
时,它会生成一个 Jar 文件 target/app-with-dependencies.jar
。这个 Jar 文件包括项目本身的编译 类 和依赖项 org.slf4j:slf4j-log4j12
和 com.myProject:utils
。它不包括 org.slf4j:slf4j-log4j12
所依赖的依赖项。
<?xml version="1.0" encoding="UTF-8"?>
<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>sub.optimal</groupId>
<artifactId>shadedemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.shade.version>2.3</maven.shade.version>
<maven.antrun.version>1.7</maven.antrun.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>com.myProject</groupId>
<artifactId>utils</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.version}</version>
<configuration>
<quiet>true</quiet>
<verbose>false</verbose>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>app-with-dependencies</finalName>
<artifactSet>
<includes>
<!--
here you define the dependencies which
you want to be included
-->
<include>org.slf4j:slf4j-log4j12:*</include>
<include>com.myProject:utils:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven.antrun.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${project.build.directory}" includes="${project.name}-*.jar" />
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我想构建项目,以便最终 jar 将所有依赖项包含在一个 jar 文件中 (如果不可能,将 类 从依赖项包含到 jar 文件中) 我关注了线程 Including dependencies in a jar with Maven 但它包括在内 我什至没有在我的 pom.xml 中提到的依赖项。这是我的 POM,它只有两个依赖项。
我希望在构建 final 时,它包含 pom 中提到的特定依赖项(类 或 jar 形式)
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.myProject</groupId>
<artifactId>utils</artifactId>
<version>1</version>
</dependency>
一种方法是使用 maven assembly 插件,pom.xml 配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.taobao.top.appstore.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
在这里你可以找到一个例子pom.xml如何用maven-shade-plugin
当您 运行 mvn package
时,它会生成一个 Jar 文件 target/app-with-dependencies.jar
。这个 Jar 文件包括项目本身的编译 类 和依赖项 org.slf4j:slf4j-log4j12
和 com.myProject:utils
。它不包括 org.slf4j:slf4j-log4j12
所依赖的依赖项。
<?xml version="1.0" encoding="UTF-8"?>
<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>sub.optimal</groupId>
<artifactId>shadedemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.shade.version>2.3</maven.shade.version>
<maven.antrun.version>1.7</maven.antrun.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>com.myProject</groupId>
<artifactId>utils</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.version}</version>
<configuration>
<quiet>true</quiet>
<verbose>false</verbose>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>app-with-dependencies</finalName>
<artifactSet>
<includes>
<!--
here you define the dependencies which
you want to be included
-->
<include>org.slf4j:slf4j-log4j12:*</include>
<include>com.myProject:utils:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven.antrun.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${project.build.directory}" includes="${project.name}-*.jar" />
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>