Without relying on any external package, how can maven generate an executable jar?
If the pom does not contain any references, just add maven-jar-plugin to the pom.
Solution process
Create a new project, only one main function class: Xixi.java, output Xixi Say: hello
The pom is configured as
<groupId>com.paxi</groupId> <artifactId>xixi</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>xixi</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
Execute mvn clean install to generate jar package xixi-1.0-SNAPSHOT.jar, execute jar command, and execute feedback error
java -jar xixi-1.0-SNAPSHOT.jarxixi-1.0-SNAPSHOT.jar has no main manifest attribute
Decompress the jar jar xvf xixi-1.0-SNAPSHOT.jar to view the corresponding file MANIFEST.MF
cat META-INF/MANIFEST.MFManifest-Version: 1.0Archiver-Version: Plexus ArchiverBuilt-By: paxiCreated-By: Apache Maven 3.3.3Build-Jdk: 1.8.0_121
Add plugin maven-jar-plugin in pom
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <mainClass>com.paxi.Xixi</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
Execute successful output
java -jar xixi-1.0-SNAPSHOT.jar
Xixi Say: hello
Then check the decompressed MANIFEST.MF file as
cat META-INF/MANIFEST.MFManifest-Version: 1.0Built-By: paxiCreated-By: Apache Maven 3.3.3Build-Jdk: 1.8.0_121Main-Class: com.paxi.Xixi //Added
Add dependencies in pom, how does maven generate executable jars?
You need to use plugins maven-dependency-plugin and maven-jar-plugin.
Solution process
Only add apache-commons package to pom
<groupId>com.paxi</groupId> <artifactId>xixi</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>xixi</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> </dependencies>
Use a class in the common package in the code
public static void main(String[] args) { String words = "Xixi Say: hello"; if (StringUtils.isNotBlank(words)) { System.out.println(words); } }An error occurred after packaging
java -jar xixi-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at com.paxi.Xixi.main(Xixi.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Add plugins in pom
<build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.paxi.Xixi</mainClass> <classpathPrefix>libs/</classpathPrefix> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependent-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>copy-dependents</id> <phase>package</phase> <goals> <goal>copy-dependents</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/libs </outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>Run again
java -jar xixi-1.0-SNAPSHOT.jarXixi Say: hello
MANIFEST.MF file is
cat META-INF/MANIFEST.MFManifest-Version: 1.0Built-By: paxiClass-Path: libs/commons-lang3-3.7.jarCreated-By: Apache Maven 3.3.3Build-Jdk: 1.8.0_121Main-Class: com.paxi.Xixi
Not only does it use dependencies in POM but also three-party jars. How does maven generate executable jars?
The compilation process requires modification of the compilation plugin maven-compiler-plugin . When running, you need to add a directory of three-party jars to execute the commands.
java -jar -Djava.ext.dirs=lib/ target/xixi-1.0-SNAPSHOT.jarXixi Say: helloPapa says:hello
Compilation and resolution process
The test code is
public static void main(String[] args) { String words = "Xixi Say: hello"; if (StringUtils.isNotBlank(words)) { System.out.println(words); } Papa.say(); }Use mvn command to execute error
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project xixi: Compilation failure
xixi/src/main/java/com/paxi/Xixi.java:[15,9] Symbol not found
Add compilation plugin to pom
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <compilerArgs> <arg>-Djava.ext.dirs=lib/</arg> </compilerArgs> </configuration> </plugin>
Package execution
java -jar target/xixi-1.0-SNAPSHOT.jarXixi Say: helloException in thread "main" java.lang.NoClassDefFoundError: com/paxi/Papa at com.paxi.Xixi.main(Xixi.java:15)Caused by: java.lang.ClassNotFoundException: com.paxi.Papa at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
Add parameters to get the result
java -jar -Djava.ext.dirs=lib/ target/xixi-1.0-SNAPSHOT.jarXixi Say: helloPapa says:hello
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.