If you create a Maven project with the defaults in Eclipse (STS to be exact), the POM will look like this.
pom.xml
<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>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
Main.java
package jp.demo;
public class Main {
public static void main(String[] args){
System.out.println("### START ###");
System.out.println("### END ###");
}
}
When I run Jar after maven install, I get angry if there is no manifest.
Command line
C:\sts\workspace\simple_maven\target>java -jar test-0.0.1-SNAPSHOT.jar
test-0.0.1-SNAPSHOT.jar does not have main manifest attribute
Generate manifest file
Therefore, specify the mainClass tag in order from the build tag as shown below. In the mainClass tag, specify the main class from the package.
pom.xml
<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>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>jp.demo.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
After maven install, when I executed it again, it ended normally.
Command line
C:\sts\workspace\simple_maven\target>java -jar test-0.0.1-SNAPSHOT.jar
### START ###
### END ###
-"Please use -source 7 or later to enable the diamond operator" -"Please use -source 8 or higher to enable lambda expressions"
Since the version of the compiler is low, let's add the following.
pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
--"Unknown Error" at the beginning of POM.xml
Let's add the following to properties as above. (It seems to be a bug of maven-jar-plugin 3.1.2. It came out in another project.)
pom.xml
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
reference: https://bugs.eclipse.org/bugs/show_bug.cgi?id=547409
Now this.
pom.xml
<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>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>jp.demo.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Recommended Posts