Mavenでライブラリを追加 Add libraries with Maven

はじめに Introduction

私は今まで, 外部のライブラリをプロジェクトに追加する時, 次の様に行っていました.

  1. ブラウザで使いたいライブラリの.jarを検索
  2. .jarをダウロード
  3. Netbeans
    "プロパティ" --> "ライブラリ" --> "JAR/フォルダを追加" --> ダウンロードした.jarを選択......

しかしこれでは, ライブラリのダウンロードや設定, ヴァージョン管理が面倒くさい, といった問題がある気がします. ライブラリの数が増えるとそれらが更に大変になるでしょう.

そこでMavenを使ってみるとライブラリの準備をとても楽に行う事ができました. NetbeansMavenを使って, ライブラリをプロジェクトへ追加する手順を紹介します.

The following is how I used to add external libraries to my projects.

  1. Search .jar file of the library I want to use using browser
  2. Download .jar file
  3. With Netbeans,
    "Properties" --> "Libraries" --> "Add JAR/Folder" --> Select downloaded .jar......

There are some problems at this method. It is bothersome to download .jar, add to my project, or manage version. The more libraries are increase, the more bothersome to do these.

However, I can add a library to my project easily with Maven now. I am introducing in this article how to add libraries to your netbeans project with Maven.

Maven とは What's Maven

MavenとはJava用プロジェクト管理ツールです. その機能の1つに依存性の管理があります. 依存性とはプロジェクトが依存するライブラリの事です. Mavenのこの機能を使うと, ライブラリのプロジェクトへの追加を簡単に行う事ができます.

Maven is a tool to manage project for Java. Dependency management is one of the function of Maven. Dependency is a library your project depends on. With Maven dependency management function, you can add some libraries to your project easily.

手順

主な手順を次に示します.

  1. Mavenプロジェクトを新規作成
  2. pom.xmlの編集
    1. 方法その1
    2. 方法その2
      • ブラウザを使ってライブラリのホームページを検索
      • pom.xmlに直接依存性の情報を挿入
  3. プロジェクトをビルド

1. Mavenプロジェクトを作成

Netbeansで"ファイル"-->"新規プロジェクト"-->"Maven"-->"Javaアプリケーション"と選択し"次へ"と進みます.
アーティファクトIDとは.jarなどのプロジェクトの成果物の名前です. グループIDとはプロジェクトを一意に識別できる様なグループの名前です. ヴァージョンとはプロジェクトのヴァージョンです. プロジェクトの情報を設定して"終了"で新規プロジェクトの作成を完了します. プロジェクトファイルにpom.xmlというファイルができます.

2. pom.xmlの編集

pom.xmlファイルはMavenプロジェクトの設定が書かれたファイルです.

ライブラリの情報をpom.xmlに追加するには次の2通りの方法があります. 私は後者をお勧めします.

2.1. 方法その1

2.1.1 ライブラリを検索

ブラウザでMaven Repository: Search/Browse/Exploreから検索し, 使いたいライブラリのアーティファクトID, グループ ID, ヴァージョンを調べます.

2.1.2 調べた情報を元に依存性を追加

Netbeansのプロジェクトの"依存性"で右クリックしてメニューから"依存性の追加..."を選択します. 調べたアーティファクトID, グループ ID, ヴァージョンを入力して"追加"を選択します. そうするとプロジェクトフォルダのpom.xml

<dependencies>
</dependencies>

のタグの間に

<dependency>
    <groupId>ライブラリのグループ ID</groupId>
    <artifactId>ライブラリのアーティファクト ID</artifactId>
    <version>ライブラリのヴァージョン</version>
</dependency>

という記述が挿入されます.

2.2. 方法その2

2.2.1 ライブラリのホームページを検索

ブラウザで使いたいライブラリのホームページを検索します. Mavenプロジェクトへのライブラリの導入に関する記述を探します. きっとそこには次の様なpom.xmlに挿入すべきテキストがあるでしょう.

<dependency>
    <groupId>ライブラリのグループ ID</groupId>
    <artifactId>ライブラリのアーティファクト ID</artifactId>
    <version>ライブラリのヴァージョン</version>
</dependency>

これをコピーします.

2.2.2 pom.xmlに直接依存性の情報を挿入

Netbeansでプロジェクトファイルのpom.xmlを開きます. そして次のタグの間にコピーした内容をペーストし保存します.

<dependencies>
</dependencies>

もしこのdependenciesタグが無い場合は

<packaging>jar</packaging>

の後にdependenciesタグを追加して下さい.

3. プロジェクトをビルド

最後にプロジェクトをビルドします. Netbeansのプロジェクトで右クリックして"依存性でビルド"を選択します. そうするとライブラリがダウンロードされます. これで追加したライブラリが使える様になります.

How to use Maven

The following is a main process to add a library to your project

  1. Make new Maven project
  2. Edit pom.xml
    1. method A
    2. method B
      • Search the website of the library using your browser
      • Add information of dependency to pom.xml directly
  3. Build your project

1. Make new Maven project

With Netbeans, go "File"-->"New project"-->"Maven"-->"Java Application", and push "Next".
Artifact ID is the name of project product like .jar file. Group ID is the unique identifier of your group . Version is a version of your project. Input these information and push "finish" to finish making new project. In project files, a file will be created with name pom.xml.

2. Edit pom.xml

In pom.xml file, settings of Maven project is written. There are 2 methods to add information of the external library to pom.xml. I recommend the second method.

2.1. Method A

2.1.1 Search library

With your browser, visit Maven Repository: Search/Browse/Explore, and check the artifact id, the group id, and the version of the library you want to add.

2.1.2 Add dependency to pom.xml

With Netbeans, right-click "dependencies" and open menu. Select "Add dependency...". Input the artifact id, the group id, and the version of the library you checked. Push "Add". And then, look at the tag as follows in pom.xml.

<dependencies>
</dependencies>

The contents as follows are added between that tag.

<dependency>
    <groupId>Group ID of the library</groupId>
    <artifactId>Artifact  ID of the library</artifactId>
    <version>Version of the library</version>
</dependency>

2.2. Method B

2.2.1 Search website of the library

With your browser, search the website of the library you want to add. Find description about adding library to Maven project. Maybe you will find the text like as follows.

<dependency>
    <groupId>Group ID of the library</groupId>
    <artifactId>Artifact ID of the library</artifactId>
    <version>Version of the library</version>
</dependency>

Copy the text you found in order to insert this text to pom.xml.

2.2.2 Insert dependency to pom.xml

With Netbeans, open pom.xml. Insert copied text between the following tag and save.

<dependencies>
</dependencies>

If there is no dependencies tag, add dependencies tag after the following tag.

<packaging>jar</packaging>

3. Build project

At the end, build your project. Right-click project and open menu. Select "Build dependencies". Downloading library will be started automatically and you can use the library.

まとめ Summary

Mavenを利用すると外部のライブラリを簡単に用意する事ができます. その仕方はライブラリのWebサイトを探し, maven用のxmlをコピーし, pom.xmlに挿入するだけです.

With Maven, you can add external library easily. All you have to do is to search website of the library, to copy xml text for Maven, and to insert it to pom.xml.