Create command line app with maven

Create a minimal Maven project

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>

Create a class appropriately ...

Main.java


package jp.demo;

public class Main {
	public static void main(String[] args){
		System.out.println("### START ###");
		System.out.println("### END ###");
	}
}

When you run ...

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

Specify mainClass

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>

When I run it again ...

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 ###

If you get an error like this ...

-"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>

If you get this error 2 ...

--"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

Finally

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

Create command line app with maven
Create a Maven project with a command
Create an app with Spring Boot 2
Create an app with Spring Boot
Create a jar file with the command
[Rails6] Create a new app with Rails [Beginner]
[Rails 5] Create a new app with Rails [Beginner]
Create Maven Project
Create a Spring Boot app development project with the cURL + tar command
Deploy Java web app to Azure with maven
I tried to create a LINE clone app
Analyze command line options with Apache's Commons CLI.
Create a Jar file with two lines of command
Create a Chat app with WebSocket (Tyrus) + libGDX + Kotlin
Create Restapi with Spring Boot ((1) Until Run of App)
Create Maven Web Project
[Rails] I tried to create a mini app with FullCalendar
Create XML-RPC API with Wicket
Change Wordpress siteurl with command
Try gRPC with Java, Maven
Execute external command with swift
What are command line arguments?
Create a playground with Xcode 12
Easy library introduction with Maven!
Create microservices with Spring Boot
Frequently used Maven command collection
I made a command line interface with WinMerge Plugin using JD-Core
Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse
Create an app catalog site using CLI for Microsoft 365 with Docker
Create a JVM for app distribution with JDK9 modules and jlink
Dealing with IO Exception: Connection reset with OWASP dependency-check Command Line Tool
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
Create a restaurant search app with IBM Watson + Gurunavi API (with source)
Command line that can create a directory structure for building a Laravel environment with Docker in one shot
How to download and run a Jar package directly from the Maven repository with just the command line