1.新建一个maven项目,在main/java中建立Main类
public class Main {
public static void main(String[] args) {
System.out.println("hello java ...");
}
}
2.添加依赖,使其成为可执行包
<build>
<plugins>
<!--打包成为可执行包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.scl.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
3.打包运行
假如我加入其它架包了 ,又该如何打包,才能运行?
<!--自己加入的架包-->
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.1</version>
</dependency>
</dependencies>
有小伙伴就说了,我依旧像前边的那种方式来打包(结果肯定是不行的)。。。
那你只需加入下边这些代码,就可以了:
<plugin>
<!--可将其他插件也加入打包,会生成lib目录-->
<!-- maven-dependency-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
代码都加完了主程序不得改进以下啊。。。
public class Main {
public static void main(String[] args) {
try {
System.out.println(Jsoup.connect("http://www.baidu.com").get().title());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("hello java ...");
}
}
现在你再试一下打包。。。
细心的小伙伴就发现了,这里多了一个lib目录。。
那就有小伙伴有疑问了,我不想要lib目录行不行:答案是当然可以
那你只需要修改一点代码就行:(将上边我们写的哪些代码注释掉,换上新的插件即可)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cn.scl.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- <plugin>-->
<!-- <!–可将其他插件也加入打包,会生成lib目录–>-->
<!-- <!– maven-dependency-plugin –>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-dependency-plugin</artifactId>-->
<!-- <version>3.6.0</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>copy-dependencies</id>-->
<!-- <phase>package</phase>-->
<!-- <goals>-->
<!-- <goal>copy-dependencies</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <outputDirectory>${project.build.directory}/lib</outputDirectory>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- <!–打包成为可执行包–>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-jar-plugin</artifactId>-->
<!-- <version>3.3.0</version>-->
<!-- <configuration>-->
<!-- <archive>-->
<!-- <manifest>-->
<!-- <addClasspath>true</addClasspath>-->
<!-- <classpathPrefix>lib/</classpathPrefix>-->
<!-- <mainClass>cn.scl.Main</mainClass>-->
<!-- </manifest>-->
<!-- </archive>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>
你再打包你会发现。。。lib目录消失了新增了一个jar文件。我们实验一下:输入cmd....
到这里就结束了,maven的实用打包方式就讲解完了。。。
完整程序:xml文件
<?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>org.example</groupId>
<artifactId>m12</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--自己加入的架包-->
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cn.scl.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- <plugin>-->
<!-- <!–可将其他插件也加入打包,会生成lib目录–>-->
<!-- <!– maven-dependency-plugin –>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-dependency-plugin</artifactId>-->
<!-- <version>3.6.0</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>copy-dependencies</id>-->
<!-- <phase>package</phase>-->
<!-- <goals>-->
<!-- <goal>copy-dependencies</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <outputDirectory>${project.build.directory}/lib</outputDirectory>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- <!–打包成为可执行包–>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-jar-plugin</artifactId>-->
<!-- <version>3.3.0</version>-->
<!-- <configuration>-->
<!-- <archive>-->
<!-- <manifest>-->
<!-- <addClasspath>true</addClasspath>-->
<!-- <classpathPrefix>lib/</classpathPrefix>-->
<!-- <mainClass>cn.scl.Main</mainClass>-->
<!-- </manifest>-->
<!-- </archive>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>
</project>
Java文件:
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package cn.scl;
import org.jsoup.Jsoup;
/**
* <p>Project: m12 - Main</p>
* <p>Powered by scl On 2023-08-01 20:21:29</p>
* <p>描述:<p>
*
* @author scl [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class Main {
public static void main(String[] args) {
try {
System.out.println(Jsoup.connect("http://www.baidu.com").get().title());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("hello java ...");
}
}