LoginSignup
6
5

More than 1 year has passed since last update.

Mavenで外部jarを扱う方法

Posted at

方法

pom.xmlでjarを指定する。jarの場所は基本的にどこでも問題ないが、一般的にはプロジェクトルート配下のlibフォルダに配置されることが多い。以下はプロジェクトルート/libに配置した際の例。

pom.xml
<dependencies>
  <dependency>
    <groupId>jp.co.mylib</groupId>
    <artifactId>mylib</artifactId>
    <version>1.2.3</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/mylib-1.2.3.jar</systemPath>
  </dependency>
</dependencies>

${project.basedir} はプロジェクトのルートを指す。

上記の設定でビルドは通るが、mvn packageを実行した際に上記で指定したライブラリが含まれないためにサーバ実行時にエラーになる。パッケージに含めるよう以下の設定を追加する。

pom.xml
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>compile</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>
6
5
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
6
5