メモ: mvn archetype cheat sheet

February 24, 2021

基本 archetype

$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart  

JavaFX

$ mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=javafx  

jar ファイルは Executable:

$ java -jar target/mvn-fx-app-1.0-SNAPSHOT.jar   

f:id:shiro_kochi:2018××××××××:plain:w100:left

Web アプリケーション

$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp  

実行するには maven-jetty-plugin を利用し、サーブレットコンテナを使う。

pom.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>com.longest_road</groupId>  
	<artifactId>mvn-web-app</artifactId>  
	<version>1.0-SNAPSHOT</version>  
	<packaging>war</packaging>  
  
	<name>mvn-web-app Maven Webapp</name>  
	<!-- FIXME change it to the project's website -->  
	<url>http://www.example.com</url>  
  
	<properties>  
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
		<maven.compiler.source>1.7</maven.compiler.source>  
		<maven.compiler.target>1.7</maven.compiler.target>  
	</properties>  
  
	<dependencies>  
		<dependency>  
			<groupId>junit</groupId>  
			<artifactId>junit</artifactId>  
			<version>4.11</version>  
			<scope>test</scope>  
		</dependency>  
	</dependencies>  
  
	<build>  
		<finalName>mvn-web-app</finalName>  
		<pluginManagement><!-- lock down plugins versions to avoid using Maven  
				defaults (may be moved to parent pom) -->  
			<plugins>  
				<plugin>  
					<groupId>org.mortbay.jetty</groupId>  
					<artifactId>maven-jetty-plugin</artifactId>  
					<version>6.1.10</version>  
					<configuration>  
						<scanIntervalSeconds>10</scanIntervalSeconds>  
						<connectors>  
							<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
								<port>8080</port>  
								<maxIdleTime>60000</maxIdleTime>  
							</connector>  
						</connectors>  
					</configuration>  
				</plugin>  
			</plugins>  
		</pluginManagement>  
	</build>  
</project>  

で、mvn jetty:run で実行:

$ mvn jetty:run  
  
...  
  
[INFO] Started SelectChannelConnector@0.0.0.0:8080  
[INFO] Started Jetty Server  
[INFO] Starting scanner at interval of 10 seconds.  

f:id:shiro_kochi:2018××××××××:plain:w100:left

JAX-RS による REST アプリケーション

$ mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp  

jersey のバージョンを 2.x に変更する:

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/maven-v4_0_0.xsd">  
  
	<modelVersion>4.0.0</modelVersion>  
  
	<groupId>com.longest_road</groupId>  
	<artifactId>mvn-rest-app</artifactId>  
	<packaging>war</packaging>  
	<version>1.0-SNAPSHOT</version>  
	<name>mvn-rest-app</name>  
  
	<build>  
		<finalName>mvn-rest-app</finalName>  
		<plugins>  
			<plugin>  
				<groupId>org.apache.maven.plugins</groupId>  
				<artifactId>maven-compiler-plugin</artifactId>  
				<version>2.5.1</version>  
				<inherited>true</inherited>  
				<configuration>  
					<source>1.8</source>  
					<target>1.8</target>  
				</configuration>  
			</plugin>  
			<plugin>  
				<groupId>org.mortbay.jetty</groupId>  
				<artifactId>maven-jetty-plugin</artifactId>  
				<version>6.1.10</version>  
				<configuration>  
					<scanIntervalSeconds>10</scanIntervalSeconds>  
					<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
						<port>8080</port>  
						<maxIdleTime>60000</maxIdleTime>  
					</connector>  
				</configuration>  
			</plugin>  
		</plugins>  
	</build>  
  
	<dependencyManagement>  
		<dependencies>  
			<dependency>  
				<groupId>org.glassfish.jersey</groupId>  
				<artifactId>jersey-bom</artifactId>  
				<version>${jersey.version}</version>  
				<type>pom</type>  
				<scope>import</scope>  
			</dependency>  
		</dependencies>  
	</dependencyManagement>  
  
	<dependencies>  
		<dependency>  
			<groupId>org.glassfish.jersey.containers</groupId>  
			<artifactId>jersey-container-servlet-core</artifactId>  
			<!-- use the following artifactId if you don't need servlet 2.x compatibility -->  
			<!-- artifactId>jersey-container-servlet</artifactId -->  
		</dependency>  
		<dependency>  
			<groupId>org.glassfish.jersey.inject</groupId>  
			<artifactId>jersey-hk2</artifactId>  
		</dependency>  
		<!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId>  
			<artifactId>jersey-media-json-binding</artifactId> </dependency> -->  
	</dependencies>  
	<properties>  
<!--  		<jersey.version>3.0.1</jersey.version> -->  
		<jersey.version>2.31</jersey.version>  
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
	</properties>  
</project>  

且つ、jakarta.ws.rs.* を javax.ws.rs.* に変更する:

MyResource.java
package com.longest_road.rest;  
  
//import jakarta.ws.rs.GET;  
//import jakarta.ws.rs.Path;  
//import jakarta.ws.rs.Produces;  
//import jakarta.ws.rs.core.MediaType;  
  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  
  
/**  
 * Root resource (exposed at "myresource" path)  
 */  
@Path("myresource")  
public class MyResource {  
  
    /**  
     * Method handling HTTP GET requests. The returned object will be sent  
     * to the client as "text/plain" media type.  
     *  
     * @return String that will be returned as a text/plain response.  
     */  
    @GET  
    @Produces(MediaType.TEXT_PLAIN)  
    public String getIt() {  
        return "Got it!";  
    }  
}  

この状態で mvn jetty:run すれば良い:

$ mvn jetty:run  
  
...  
  
[INFO] Started SelectChannelConnector@0.0.0.0:8080  
[INFO] Started Jetty Server  
[INFO] Starting scanner at interval of 10 seconds.  

f:id:shiro_kochi:2018××××××××:plain:w100:left

Spring Boot アプリケーション

$ mvn archetype:generate -DarchetypeGroupId=org.springframework.boot -DarchetypeArtifactId=spring-boot-sample-jetty-archetype  
$ mvn package  
$ ls target/mvn-spring-app-1.0-SNAPSHOT.jar*  
target/mvn-spring-app-1.0-SNAPSHOT.jar		target/mvn-spring-app-1.0-SNAPSHOT.jar.original  

上記の target/mvn-spring-app-1.0-SNAPSHOT.jar はランタイムである Jetty その他ライブラリを全て含んだ jar ファイルとなり、これ単体でアプリケーションの実行が可能。
target/mvn-spring-app-1.0-SNAPSHOT.jar.original はプロジェクトに含まれるプログラムのみの jar ファイルとなり、サーブレットコンテナにデプロイして利用する。

$ java -jar target/mvn-spring-app-1.0-SNAPSHOT.jar  
  
  .   ____          _            __ _ _  
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \  
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \  
 \\/  ___)| |_)| | | | | || (_| | ))))  
  '  |____| .__|_| |_|_| |_\__, | / / / /  
 =========|_|==============|___/=/_/_/_/  
 :: Spring Boot ::        (v1.0.2.RELEASE)  

f:id:shiro_kochi:2018××××××××:plain:w100:left


 © 2023, Dealing with Ambiguity