maven 打包,都不带依赖第三jar:maven-assembly-plugin
不加main类而带依赖的jar包
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</project>
加main类也带依赖的jar包
<project>
<build>
<plugins>
<!--(start) for package jar with dependencies -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!--(end) for package jar with dependencies -->
</plugins>
</build>
</project>
打包
使用以下命令打包:
mvn package
How To Exclude Dependencies from Maven Assembly Plugin
Any dependencies that are already available at the runtime environment can be given the scope of provided
as highlighted below.
<scope>provided</scope>
使用scope将不需要打入的jar排除
Maven依赖Scope标签用法
在一个maven项目中,如果存在编译需要而发布不需要的jar包,可以用scope标签,值设为provided。如下:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
<classifier />
</dependency>
scope的其他参数如下:
- compile
默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布 - provided
跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。???????? - runtime
表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段。 - test
表示dependency作用在测试时,不作用在运行时。 只在测试时使用,用于编译和运行测试代码。不会随项目发布。 - system
跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。