본문 바로가기

Maven

Maven Compile, 실행

실습 환경

- macOS (11.2.1)

- Visual Studio Code

- Open JDK 11

- Apache Maven 3.8.1


0. 목표

Maven Project를 생성, 컴파일, 실행할 수 있다.

1. Maven Project 생성

Visual Studio Code 에서 Command Palette (fn + F1) 을 열어 Maven Project를 생성한다.

Maven Project 생성

archetype 설정

  • archetype은 Maven에서 제공하는 프로젝트 템플릿으로, 만들고자 하는 프로젝트 타입의 모범사례를 제공한다.
  • maven-archetype-quickstart, version은 1.4 를 선택한다.

archetype 설정

groupId 설정

  • com.example 로 설정한다.

artifactId

- demo 로 설정한다.

2. Project 구조

Project 구조

구성요소 설명
src/main/java Application/Library 소스코드 작성하는 곳
src/test/java 테스트 소스코드 작성
target 빌드 결과물을 보관
(보이지 않는다면, pom.xml이 있는 곳에서 mvn compile 실행)
pom.xml 프로젝트의 최상위에 위치

3. pom.xml

pom (Project Object Model) 은 Maven의 기본 작업 단위이다. 프로젝트를 빌드하기 위해 Maven에서 사용하는 프로젝트 및 구성 세부사항이 포함된 XML파일이다. Maven은 현재 디렉터리에서 pom.xml 을 찾아 필요한 구성 정보를 얻어 build 한다.

maven-archetype-quickstart 으로 프로젝트를 생성하면 아래와 같이 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.example</groupId>
  <artifactId>demo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>demo</name>

  <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>
    <!-- plugin 생략 -->
  </build>
</project>

properties, dependencies, build는 앞으로 직접 작성할 예정이므로 삭제한다.

4. compile

pom.xml 이 위치한 곳에서 mvn compile 명령어를 실행한다. 명령어를 실행하면 아래와 같이 빌드를 실패했다는 메세지가 나온다.

[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory {path}
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to {path}
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[INFO] 2 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.462 s
[INFO] Finished at: 2021-05-23T01:27:25+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project demo: Compilation failure: Compilation failure: 
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Maven 3.8.1 에서 제공하는 compiler plugin 은 jdk7 이상에서 사용할 수 있다고 나온다. jdk version 을 직접 설정해서 해결할 수 있다.

maven-compiler-plugin 설정

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

  ...

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

위와 같이 maven-compiler-plugin 을 추가하고 태그하위에 jdk 버전을 입력한다.

입력 후 mvn compile 명령어를 다시 실행하면 컴파일이 잘 되는것을 확인할 수 있다. 컴파일된 결과는 target/classes/com/example/App.class 에서 확인할 수 있다.

5. com.example.App.class 실행

java 명령어로 실행

target/classes 로 이동해 아래 명령어를 입력한다.

java com.example.App

실행 결과

Maven Plugin 으로 실행

exec-maven-plugin 을 사용해 App.class 를 실행할 수 있다.

참고 url : https://www.mojohaus.org/exec-maven-plugin/

dependency 추가

아래 링크를 참조해 dependency를 추가한다.

https://mvnrepository.com/artifact/org.codehaus.mojo/exec-maven-plugin/3.0.0

<project>
  ...
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.codehaus.mojo/exec-maven-plugin -->
    <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
    </dependency>
  </dependencies>
  ...
</project>

build plugin 설정

<project>

  ...
  <dependencies> ... </dependencies>

  <build>
    <plugins>
      <plugin> (maven-compiler-plugin) </plugin>

      <plugin>
          <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <mainClass>com.example.App</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

태그에 를 설정한다. package 를 포함한 경로를 입력한다.

pom.xml 이 위치한 경로에서 다음과 같이 입력하면 실행된다.

mvn exec:java

mvn exec:java 실행결과

 

아래와 같이 입력하면 compile 한 후에 바로 컴파일 결과를 실행할 수 있다.

mvn compile exec:java

 

 

 

6. 맺음말

maven project를 생성해 소스코드를 compile하고, 실행해봤다.

'Maven' 카테고리의 다른 글

의존하는 라이브러리를 포함해 jar를 만드는 방법  (1) 2021.08.21
Launch4j 사용 방법  (1) 2021.06.16
Maven maven-jar-plugin  (0) 2021.06.15