LoginSignup
9
7

More than 5 years have passed since last update.

IntelliJで実行可能なjarファイルをmavenプロジェクトで作る

Posted at

概要

普段作る事の無いjarファイルをIntelliJから作る機会があり、すこしはまったのでログとして残しておきます。
※尚、僕のIntelliJはver15です。

プロジェクトの作成

File > New > Project で New Project のウィンドウを出して、Mavenを指定してプロジェクトを作ります。

ソースの記述

適当にソースを記述します。
src/main/java配下にとりあえず以下のようなクラスを作りました。

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("HelloWorld");
    }
}

また、ここで念のため実行して問題が無い事を確認しておきます。

maven-assembly-pluginでjarファイルを作成出来るようにする

<?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>testjar</groupId>
    <artifactId>testjar</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- ここから -->
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <!-- mainクラスを指定する -->
                            <mainClass>HelloWorld</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- ここまでコピペ -->
</project>

mvn package

Maven Projects > ${yourprojectname} > Lifecycle > package の実行

実行後プロジェクトルートのtarget配下にjarファイルが作成されている事が確認出来ます。

9
7
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
9
7