読者です 読者をやめる 読者になる 読者になる

Azureの小ネタ (改)

~Azureネタを中心に、色々とその他の技術的なことなどを~

mavenでJettyをさくっと動作させるまで

mavenでJettyを実行するところまでの備忘録。実行には、jetty-maven-pluginを使います。

必要なもの

  • Java 8
  • Maven 3.3
  • Windows (環境でやってます)

Javaを設定しJAVA_HOMEを設定後、パスに追加する。mavenを解凍し、maven/bin もパスに追加する。

ちなみに、maven-jetty-pluginは以下を参照。

WebApps プロジェクト作成

以下のmvnコマンドを実行すると、test-serverフォルダが作成される

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=test.webapp -DartifactId=test-server -Dpackage=test.webapp -DinteractiveMode=false

以下のように出力される

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: c:\Workspace\eclipse\ws-p\ztest
[INFO] Parameter: package, Value: test.webapp
[INFO] Parameter: groupId, Value: test.webapp
[INFO] Parameter: artifactId, Value: test-server
[INFO] Parameter: packageName, Value: test.webapp
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: c:\Workspace\eclipse\ws-p\ztest\test-server
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.457 s
[INFO] Finished at: 2015-08-23T18:19:26+09:00
[INFO] Final Memory: 13M/193M
[INFO] ------------------------------------------------------------------------

ディレクトリ構成は以下のとおり。

C:.
└─src
    └─main
        ├─resources
        └─webapp
            └─WEB-INF

pom.xml の作成

以下のpom.xmlを用意する。jetty.versionは適宜修正。

dependencyは、以下を追加する。 * jetty-server * jetty-webapp

pluginには、以下を追加する。 * jetty-maven-plugin

<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>test.webapp</groupId>
  <artifactId>test-server</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test-server Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <jetty.version>9.3.2.v20150730</jetty.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jetty.version}</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-webapp</artifactId>
      <version>${jetty.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>test-server</finalName>
    <plugins>
      <plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
      </plugin>
    </plugins>
  </build>
</project>

インストール&実行

インストールを実行すると色々追加される。

mvn install
C:.
├─src
│  └─main
│      ├─resources
│      └─webapp
│          └─WEB-INF
└─target
    ├─classes
    ├─maven-archiver
    └─test-server
        ├─META-INF
        └─WEB-INF
            ├─classes
            └─lib

jettyを実行する。

mvn jetty:run

ログが表示され http://localhost:8080/ にアクセスしてHello Worldが表示されればOK。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-server Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> jetty-maven-plugin:9.3.2.v20150730:run (default-cli) > test-compile @ test-server >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test-server ---
[WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test-server ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test-server ---
[WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\Workspace\eclipse\ws-p\ztest\test-server\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test-server ---
[INFO] No sources to compile
[INFO]
[INFO] <<< jetty-maven-plugin:9.3.2.v20150730:run (default-cli) < test-compile @ test-server <<<
[INFO]
[INFO] --- jetty-maven-plugin:9.3.2.v20150730:run (default-cli) @ test-server ---
[INFO] Configuring Jetty for project: test-server Maven Webapp
[INFO] webAppSourceDirectory not set. Trying src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\Workspace\eclipse\ws-p\ztest\test-server\target\classes
[INFO] Logging initialized @4335ms
[INFO] Context path = /