1.说明
本文的打包操作适用于 普通的java项目
。
本文介绍的 maven 的打包方式是把java项目的第三方依赖包与主程序独立出来,即目标jar包与依赖包目录同级。
本文介绍的项目中包含自己写的本地的第三方依赖包。
2.具体的操作
2.1 项目结构介绍
一个标准的maven项目
: 目录结构完全符合maven规范
【注意】:自己的第三方依赖包的名称中一定要包含一个 版本号的信息,这样在pom.xml中配置时可以使用到。 如 : java-utile-from-northcastle-1.0.jar
中的 -1.0
helloworld-maven-java| -- src| -- main| -- java| -- com.northcastle| -- App.java : 主类| -- resources| -- libs| -- java-utile-from-northcastle-1.0.jar| -- test| -- java| -- com.northcastle| -- AppTest.java| -- resources|-- pom.xml
App.java 的文件内容:
package com.northcastle;import com.alibaba.fastjson.JSONObject;
import com.northcastle.utile.a.UtileA;
import com.northcastle.utile.b.UtileB;
public class App {public static void main( String[] args ){System.out.println("=== App run begin ===");System.out.println( "Hello World!" );System.out.println( "欢迎使用maven进行打包工作" );JSONObject jsonObject = new JSONObject();jsonObject.put("name","northcastle");System.out.println("jsonObject is : "+jsonObject);UtileA.sayHelloA(); UtileB.sayHelloB(); System.out.println("=== App run end ===");}
}
2.2 pom.xml文件内容(核心)
pom.xml 中主要 包含了引入的第三方的依赖包(包含自己写的第三方依赖包)、以及跳过测试的配置、打包的配置。
具体内容如下:
<project xmlns&#61;"http://maven.apache.org/POM/4.0.0" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.northcastlegroupId>
<artifactId>helloworld-maven-javaartifactId>
<version>1.0-SNAPSHOTversion><packaging>jarpackaging>
<properties><maven.compiler.source>1.8maven.compiler.source><maven.compiler.target>1.8maven.compiler.target><maven.compiler.encoding>UTF-8maven.compiler.encoding><project.build.sourceEncoding>UTF-8project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>properties>
<dependencies><dependency><groupId>com.alibabagroupId><artifactId>fastjsonartifactId><version>1.2.75version>dependency><dependency><groupId>junitgroupId><artifactId>junitartifactId><version>4.11version><scope>testscope>dependency><dependency><groupId>com.northcastlegroupId><artifactId>java-utile-from-northcastleartifactId><version>1.0version><scope>systemscope><systemPath>${project.basedir}/src/main/resources/libs/java-utile-from-northcastle-1.0.jarsystemPath>dependency>dependencies><build><finalName>helloworld-maven-javafinalName><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-surefire-pluginartifactId> <configuration><skip>trueskip>configuration>plugin><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-jar-pluginartifactId><configuration><classesDirectory>target/classesclassesDirectory><archive><manifest><mainClass>com.northcastle.AppmainClass><useUniqueVersions>falseuseUniqueVersions><addClasspath>trueaddClasspath><classpathPrefix>libs/classpathPrefix>manifest><manifestEntries><Class-Path>.Class-Path><Class-Path>libs/java-utile-from-northcastle-1.0.jarClass-Path>manifestEntries>archive>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><type>jartype><includeTypes>jarincludeTypes><outputDirectory>${project.build.directory}/libsoutputDirectory>configuration>execution>executions>plugin>plugins>
build>project>
2.2.1 pom.xml 中表示指定程序入口类的配置
这一块是配置的核心&#xff1a;
最特殊的是 &#xff1a;配置中的这一句话 &#xff1a;
当有多个本地的jar包时&#xff0c;用空格
进行分开。
Class-Path标签只能有一个
<manifestEntries><Class-Path> . libs/java-utile-from-northcastle-1.0.jar xxxxx.jarClass-Path>manifestEntries>
<plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-jar-pluginartifactId><configuration><classesDirectory>target/classesclassesDirectory><archive><manifest><mainClass>com.northcastle.AppmainClass><useUniqueVersions>falseuseUniqueVersions><addClasspath>trueaddClasspath><classpathPrefix>libs/classpathPrefix>manifest><manifestEntries><Class-Path>. libs/java-utile-from-northcastle-1.0.jarClass-Path>manifestEntries>archive>configuration>plugin>
2.2.2 pom.xml 中表示第三方依赖的配置
这一块是配置的核心 &#xff1a; 如果不理解&#xff0c;直接抄上就行
<plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-dependency-pluginartifactId><executions><execution><id>copy-dependenciesid><phase>packagephase><goals><goal>copy-dependenciesgoal>goals><configuration><type>jartype><includeTypes>jarincludeTypes><outputDirectory>${project.build.directory}/libsoutputDirectory>configuration>execution>executions>plugin>
2.3 执行打包命令
在 pom.xml 文件所在的目录下执行打包命令 &#xff1a; mvn clean package
2.4 查看打包后target目录
根据我们的pom.xml 中的配置&#xff0c;打包成功后:
1.会在 target目录
下生成一个 libs
的目录来存放我们的第三方的依赖包&#xff1b;
2.会在 target目录
下生成我们的目标jar包
。
2.5 运行jar包&#xff0c;检查效果
使用java -jar xxx.jar
命令&#xff0c;运行程序
》 ps : 如果各位coder出现打包成功&#xff0c;但在运行时报 ClassNotFoundException
的异常&#xff0c;请仔细阅读本篇文章的2.2节
&#xff0c;会给你启发的。
3.完成
Congratulations!
You are one step closer to success!