Java学习之:如何将 java 程序打包成 .jar 文件_暖仔会飞的博客-CSDN博客_java文件打包成jar
Javaweb项目导出成jar包并使用Windows定时任务定时执行 - 信铁寒胜 - 博客园
Maven 各种花式构建,不用 SpringBoot 也能打出可执行 Jar 包 - 掘金
mvn打jar包示例:依赖打入jar包和依赖打到外部文件夹_一首简单的歌-shining的博客-CSDN博客
=================mvn打jar包示例:依赖打入jar包和依赖打到外部文件夹
使用maven打包java的jar包时,通常有两种情况:
将依赖打到外部文件夹,将源码单独打jar包运行;
将依赖和源码一起打到jar包中运行。
下面举例说明这两种情况:
建立如下测试类,依赖一个common-lang包(用于测试外部依赖):
- package mvnDemo;
-
- import org.apache.commons.lang3.StringUtils;
-
- public class MvnDemo {
-
- public static void main(String[] args) {
- System.out.println(StringUtils.upperCase("hello mvn"));
- }
- }
-
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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>testDemoartifactId>
- <version>1.0-SNAPSHOTversion>
-
- <dependencies>
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-lang3artifactId>
- <version>3.11version>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>RELEASEversion>
- <scope>compilescope>
- dependency>
-
- dependencies>
- <build>
- <plugins>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <configuration>
- <source>8source>
- <target>8target>
- configuration>
- plugin>
-
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-jar-pluginartifactId>
- <version>3.2.0version>
- <configuration>
- <archive>
- <manifest>
- <mainClass>
- mvnDemo.MvnDemo
- mainClass>
- <addClasspath>
- true
- addClasspath>
- <classpathPrefix>
- lib
- classpathPrefix>
- manifest>
- archive>
- configuration>
- plugin>
-
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-dependency-pluginartifactId>
- <version>3.2.0version>
- <executions>
- <execution>
- <id>copy-dependenciesid>
- <phase>packagephase>
- <goals>
- <goal>copy-dependenciesgoal>
- goals>
- <configuration>
- <outputDirectory>${project.build.directory}/liboutputDirectory>
- configuration>
- execution>
- executions>
- plugin>
-
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-shade-pluginartifactId>
- <version>3.2.4version>
- <executions>
- <execution>
- <phase>packagephase>
- <goals>
- <goal>shadegoal>
- goals>
- <configuration>
- <filters>
- <filter>
- <artifact>*:*artifact>
- <excludes>
- <exclude>META-INF/*.SFexclude>
- <exclude>META-INF/*.DSAexclude>
- <exclude>META-INF/*.RSAexclude>
- excludes>
- filter>
- filters>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>mvnDemo.MvnDemomainClass>
- transformer>
- transformers>
- configuration>
- execution>
- executions>
- plugin>
- plugins>
- build>
-
- project>
-
打完的jar如下:
lib中存放的是相关依赖jar包(比如common-lang包);
original-testDemo-1.0-SNAPSHOT.jar是仅含源码的jar包;
testDemo-1.0-SNAPSHOT.jar是既包含源码又包含相关依赖的jar包。
上述两个jar包分别使用java -jar original-testDemo-1.0-SNAPSHOT.jar java -jar testDemo-1.0-SNAPSHOT.jar运行都能得到
HELLO MVN
的运行结果。
————————————————
- <properties>
-
- <output.dependence.file.path>lib/output.dependence.file.path>
- <output.resource.file.path>resource/output.resource.file.path>
- <output.jar.name>wen-toolsoutput.jar.name>
- <output.jar.main>com.wen.Mainoutput.jar.main>
- properties>
-
-
-
- <build>
- <finalName>${output.jar.name}finalName>
- <plugins>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-jar-pluginartifactId>
- <configuration>
-
- <excludes>
- <exclude>*.propertiesexclude>
- <exclude>*.ymlexclude>
- <exclude>*/*.propertiesexclude>
- <exclude>*/*.ymlexclude>
- excludes>
- <archive>
- <manifest>
- <addClasspath>trueaddClasspath>
-
-
- <classpathPrefix>${output.dependence.file.path}classpathPrefix>
-
- <useUniqueVersions>falseuseUniqueVersions>
-
- <mainClass>${output.jar.main}mainClass>
- manifest>
- <manifestEntries>
-
-
- <Class-Path>./${output.resource.file.path}Class-Path>
- manifestEntries>
- archive>
- <outputDirectory>${project.build.directory}outputDirectory>
- configuration>
- plugin>
-
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-dependency-pluginartifactId>
- <executions>
- <execution>
- <id>copy-dependenciesid>
- <phase>packagephase>
- <goals>
- <goal>copy-dependenciesgoal>
- goals>
- <configuration>
- <outputDirectory>${project.build.directory}/${output.dependence.file.path}outputDirectory>
- configuration>
- execution>
- executions>
- plugin>
-
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <executions>
-
- <execution>
- <id>copy-resourcesid>
- <phase>packagephase>
- <goals>
- <goal>copy-resourcesgoal>
- goals>
- <configuration>
- <resources>
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>*.propertiesinclude>
- <include>*.ymlinclude>
- <include>*/*.propertiesinclude>
- <include>*/*.propertiesinclude>
- includes>
- resource>
- resources>
- <outputDirectory>${project.build.directory}/${output.resource.file.path}outputDirectory>
- configuration>
- execution>
- executions>
- plugin>
-
-
-
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
-
- <includes>
- <include>
- <groupId>nullgroupId>
- <artifactId>nullartifactId>
- include>
- includes>
- <outputDirectory>${project.build.directory}outputDirectory>
- configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackagegoal>
- goals>
- <configuration>
-
-
-
- configuration>
- execution>
- executions>
- plugin>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-surefire-pluginartifactId>
- <configuration>
- <skip>trueskip>
- configuration>
- plugin>
-
- plugins>
- build>
----------------------------------------
Java——maven打完jar包之后将jar包放到指定位置总结_前方一片光明的博客-CSDN博客_maven编译后的jar包放在哪里
Maven打包到指定位置_AllenLeungX的博客-CSDN博客_maven打包指定文件目录