LoginSignup
35
34

More than 5 years have passed since last update.

JavaEE7環境を構築する

今回は以下の様な構成を考えています。

  • Java8
  • JavaEEコンテナはWildFly(http://wildfly.org)
  • 開発環境はEclipse(Luna)
  • Mavenを使う(今回はGradleは使わない)

JavaEE7に対応しているコンテナは、WildFlyとGlassfishがありますが、今回はWildFlyを使うことにします。(理由は、直近で使う機会があるため)

JavaEE7の仕様

  • JSR 236: Concurrency Utilities for JavaEE 1.0
  • JSR 338: Java Persistence API 2.1 (JPA)
  • JSR 339: JavaAPI for RESTful WebServices 2.0 (JAX-RS)
  • JSR 340: Java Servlet 3.1
  • JSR 341: Expression Language 3.0 (EL)
  • JSR 342: JavaPlatform, EnterpriseEdition 7
  • JSR 343: Java Message Service 2.0 (JMS)
  • JSR 344: JavaServer Faces 2.2 (JSF)
  • JSR 345: Enterprise JavaBeans 3.2 (EJB)
  • JSR 346: Contextsand Dependency Injection for JavaEE 1.1 (CDI)
  • JSR 349: Bean Validation 1.1
  • JSR 352: BatchApplications for the JavaPlatform 1.0
  • JSR 353: JavaAPI for JSON Processing 1.0
  • JSR 356: JavaAPI for WebSocket 1.0

javaee7-pancake-1024x450.png

Eclipseの環境構築

WildFlyをインストール

Eclipse Marketplace から JBoss Tools(Luna)4.2.3.Final をインストールします。

Featuresはすべて選択すればOKです。

Preferences -> Server -> Runtime Enviroments
Addボタンを押し、JBoss Community -> WildFly 8.x Runtime

まだWildFlyをインストールしていない方は、任意の場所にダウンロード可能です。

Runtime JREにはJava8を選択してください。

WildFlyの実装

JSR COMPONENT IMPLEMENTATION URL
JSR 236 Concurrency Utilities for JavaEE 1.0 Used from GlassFish
JSR 338 Java Persistence API 2.1 (JPA) Hibernate http://hibernate.org
JSR 339 JavaAPI for RESTful WebServices 2.0 (JAX-RS) RESTEasy http://resteasy.jboss.org
JSR 340 Java Servlet 3.1 Undertow http://undertow.io
JSR 341 Expression Language 3.0 (EL) Used from GlassFish
JSR 342 JavaPlatform, EnterpriseEdition 7
JSR 343 Java Message Service 2.0 (JMS) HornetQ http://hornetq.jboss.org
JSR 344 JavaServer Faces 2.2 (JSF) Used from GlassFish
JSR 345 Enterprise JavaBeans 3.2 (EJB) WildFly http://wildfly.org
JSR 346 Contextsand Dependency Injection for JavaEE 1.1 (CDI) Weld http://weld.cdi-spec.org
JSR 349 Bean Validation 1.1 Hibernate Validator http://hibernate.org/subprojects/validator.html
JSR 352 BatchApplications for the JavaPlatform 1.0 jberet https://github.com/jberet/jsr352
JSR 353 JavaAPI for JSON Processing 1.0 Used from GlassFish
JSR 356 JavaAPI for WebSocket 1.0 Undertow (lightweight Web server for WildFly) http://undertow.io
JSR 322 Java Connector Architecture IronJacamar http://www.ironjacamar.org
JSR 907 Java Transaction API Narayana http://narayana.io
JSR 224 Java API for XML-based Web Services JBossWS(Apache CFX is Default) http://jbossws.jboss.org

GlassFishをインストール(GlassFishが良い方はこちら)

Eclipse Marketplace から GlassFish Tools をインストールします。

Featuresはすべて選択すればOKです。

Preferences -> Server -> Runtime Enviroments
Addボタンを押し、GlassFish -> GlassFish 4

GlassFishを使用する際は、事前にGlassFishのインストールをしておく必要があります。

Runtime JREにはJava8を選択してください。

GlassFish の実装

JSR COMPONENT IMPLEMENTATION URL
JSR 236 Concurrency Utilities for JavaEE 1.0
JSR 338 Java Persistence API 2.1 (JPA) EclipseLink http://www.eclipse.org/eclipselink/
JSR 339 JavaAPI for RESTful WebServices 2.0 (JAX-RS) Jersey https://jersey.java.net
JSR 340 Java Servlet 3.1
JSR 341 Expression Language 3.0 (EL)
JSR 342 JavaPlatform, EnterpriseEdition 7
JSR 343 Java Message Service 2.0 (JMS) Open Message Queue https://mq.java.net
JSR 344 JavaServer Faces 2.2 (JSF) Mojarra JavaServer Faces https://javaserverfaces.java.net
JSR 345 Enterprise JavaBeans 3.2 (EJB)
JSR 346 Contextsand Dependency Injection for JavaEE 1.1 (CDI)
JSR 349 Bean Validation 1.1
JSR 352 BatchApplications for the JavaPlatform 1.0
JSR 353 JavaAPI for JSON Processing 1.0
JSR 356 JavaAPI for WebSocket 1.0
JSR 322 Java Connector Architecture
JSR 907 Java Transaction API
JSR 224 Java API for XML-based Web Services

プロジェクトを作成

Mavenを使用して、JavaEE7プロジェクトを作成します。

mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes \
-DarchetypeArtifactId=webapp-javaee7 \
-DarchetypeVersion=1.1 \
-DgroupId=sample \
-DartifactId=javaee7-sample \
-Dversion=1.0.0-SNAPSHOT \
-Dpackage=sample   \
-Darchetype.interactive=false \
--batch-mode --update-snapshots archetype:generate

% mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes \
> -DarchetypeArtifactId=webapp-javaee7 \
> -DarchetypeVersion=1.1 \
> -DgroupId=sample \
> -DartifactId=javaee7-sample \
> -Dversion=1.0.0-SNAPSHOT \
> -Dpackage=sample   \
> -Darchetype.interactive=false \
> --batch-mode --update-snapshots archetype:generate
[INFO] Scanning for projects...
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (20 KB at 24.0 KB/sec)
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (13 KB at 15.1 KB/sec)
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (730 B at 2.7 KB/sec)
[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] Archetype repository not defined. Using the one from [org.codehaus.mojo.archetypes:webapp-javaee7:1.1] found in catalog remote
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-javaee7/1.1/webapp-javaee7-1.1.jar
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-javaee7/1.1/webapp-javaee7-1.1.jar (2 KB at 6.5 KB/sec)
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-javaee7/1.1/webapp-javaee7-1.1.pom
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-javaee7/1.1/webapp-javaee7-1.1.pom (957 B at 3.6 KB/sec)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: webapp-javaee7:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: sample
[INFO] Parameter: artifactId, Value: javaee7-sample
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] Parameter: package, Value: sample
[INFO] Parameter: packageInPathFormat, Value: sample
[INFO] Parameter: package, Value: sample
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] Parameter: groupId, Value: sample
[INFO] Parameter: artifactId, Value: javaee7-sample
[INFO] project created from Archetype in dir: /Users/xxxxx/Desktop/javaee7-sample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.744 s
[INFO] Finished at: 2015-06-18T00:18:13+09:00
[INFO] Final Memory: 12M/121M
[INFO] ------------------------------------------------------------------------

Mavenプロジェクトが作成されました。

% tree javaee7-sample 
javaee7-sample
├── pom.xml
└── src
    └── main
        ├── java
        │   └── sample
        └── webapp
            └── index.html

5 directories, 2 files

Eclipseに取り込む

このまま取り込んでも良いのですが、コンパイラの設定が Java7を使うようになっています。
Java8を使用するように設定の変更を行います。

Java8 に変更

pom.xml
           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>

33行目、34行目を 1.7から1.8に変更します。

Eclipseに取り込む

Eclipse -> Import -> Maven -> Exsiting Maven Project
先ほど作成したmy-webappを取り込みます。

エラーを解決

インポートを行うと、Mavenでエラーが出ています。
エラーを解決するために、pom.xml を修正します。
スクリーンショット 2015-06-18 00.48.38.png

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>sample</groupId>
    <artifactId>javaee7-sample</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>javaee7-sample</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
           </plugin>
        </plugins>
    </build>

</project>


pom.xmlを保存したら、javaee7-sampleを右クリックして
Maven -> Update Project を実行。

※他にも方法はあるかもしれませんが、今回はこのように対応しました。。。良い方法があれば教えて下さい。

スクリーンショット 2015-06-18 00.52.06.png

実行する

スクリーンショット 2015-06-18 00.53.20.png

スクリーンショット 2015-06-18 00.53.30.png

スクリーンショット 2015-06-18 00.53.41.png

スクリーンショット 2015-06-18 00.56.25.png

まとめ

まずは、環境構築ということでWildFly上でMavenプロジェクトを実行することができました。今後は、この環境をベースにJavaEE7について説明していきたいと思います。

おそらく、GlassFishでも動くと思います。

NetBeansの方が簡単に環境を作れるのかもしれませんが、それでもEclipse派!という方の参考になればと思います。

35
34
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
34