2013년 8월 17일 토요일

Maven을 이용한 샘플 Web 빌드 및 컴파일러 플러그인 구성

1. Web 애플리케이션을 작성할 폴더에서 Archetype에 기초해 프로젝트 구조를 작성합니다.

-> C:\Users\kdyoung\web>mvn archetype:generate

500: remote -> org.codehaus.mojo.archetypes:webapp-j2ee13 (-)
501: remote -> org.codehaus.mojo.archetypes:webapp-j2ee14 (-)
502: remote -> org.codehaus.mojo.archetypes:webapp-javaee6 (-)
503: remote -> org.codehaus.mojo.archetypes:webapp-javaee7 (Archetype for a web application using Java EE 7.)
504: remote -> org.codehaus.mojo.archetypes:webapp-jee5 (-)
-> Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 294: 501

Choose org.codehaus.mojo.archetypes:webapp-j2ee14 version:
1: 1.0
2: 1.0.1
3: 1.1
4: 1.2
5: 1.3
-> Choose a number: 5: 엔터키

-> Define value for property 'groupId': : org.samples.maven
-> Define value for property 'artifactId': : mywebapp
-> Define value for property 'version':  1.0-SNAPSHOT: : 엔터키
-> Define value for property 'package':  org.samples.maven: : 엔터키
Confirm properties configuration:
groupId: org.samples.maven
artifactId: mywebapp
version: 1.0-SNAPSHOT
package: org.samples.maven
-> Y: : 엔터키

-> C:\Users\kdyoung\web>cd mywebapp
-> C:\Users\kdyoung\web\mywebapp>dir

2013-08-17  오전 10:19             1,218 pom.xml
2013-08-17  오전 10:19    <DIR>          src

2. pom.xml 파일 내용을 살펴 봅니다.

C:\Users\kdyoung\web\mywebapp\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>org.samples.maven</groupId>
  <artifactId>mywebapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>mywebapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.4</source>
          <target>1.4</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
---------------------------------------------------------------------------------------------------------

3. 샘플 JSP 파일을 살펴봅니다.
C:\Users\kdyoung\web\mywebapp\src\main\webapp\index.jsp
---------------------------------------------------------------------------------------------------------
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>
---------------------------------------------------------------------------------------------------------

4. 샘플 web.xml 파일을 살펴봅니다.
C:\Users\kdyoung\web\mywebapp\src\main\webapp\WEB-INF\web.xml
---------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>mywebapp</display-name>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
---------------------------------------------------------------------------------------------------------

5. 샘플을 빌드합니다.
-> C:\Users\kdyoung\web\mywebapp>mvn package
[INFO] Packaging webapp
[INFO] Assembling webapp [mywebapp] in [C:\Users\kdyoung\web\mywebapp\target\mywebapp-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Users\kdyoung\web\mywebapp\src\main\webapp]
[INFO] Webapp assembled in [105 msecs]
[INFO] Building war: C:\Users\kdyoung\web\mywebapp\target\mywebapp-1.0-SNAPSHOT.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

6. Tomcat을 실행한 뒤 mywebapp를 Deploy합니다.



7. mywebapp 사이트로 이동합니다.


Maven은 빌드 관련 플러그인들로 빌드를 수행하며 각각의 플러그인에 대한 환경 구성이 가능합니다. Archetype 선택 후 생성된 pom.xml에서 compiler는 JDK 1.4 기준으로 구성되어 있어 generic 구문을 사용시 에러가 발생하게 됩니다.

---------------------------------------------------------------------------------------------------------
참고: http://maven.apache.org/plugins/maven-compiler-plugin/index.html

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and targetas described in Setting the -source and -target of the Java Compiler.
---------------------------------------------------------------------------------------------------------


8. MyClass.java 파일을 작성합니다.
C:\Users\kdyoung\web\mywebapp\src\main\java\org\samples\maven\MyClass.java
---------------------------------------------------------------------------------------------------------
package org.samples.maven;

import java.util.*;

public class MyClass {

public static void main(String[] args)
{
List<String> stringList = new ArrayList<String> ();
}
}


---------------------------------------------------------------------------------------------------------

9. 컴파일을 시도합니다.

-> C:\Users\kdyoung\web\mywebapp>mvn compile

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.
0.2:compile (default-compile) on project mywebapp: Compilation failure
[ERROR] C:\Users\kdyoung\web\mywebapp\src\main\java\org\samples\maven\MyClass.java:[9,6] generics are not supported in -source 1.4
[ERROR] (use -source 5 or higher to enable generics)
[ERROR] List<String> stringList = new ArrayList<String>();

10. pom.xml 파일을 수정합니다.

(방법 1) 아래와 같이 source/target 값을 1.4에서 1.5로 수정합니다.
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

(방법 2) <configuration> 또는 <plugin> 또는 또는 <plugins> 또는 <build> block을 제거합니다. 디폴트 구성으로 1.5를 사용하기 때문입니다.

11. 컴파일을 합니다.

-> C:\Users\kdyoung\web\mywebapp>mvn compile

[INFO] Compiling 1 source file to C:\Users\kdyoung\web\mywebapp\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

댓글 없음:

댓글 쓰기