既知なやり方かもしれないけど、ハマったのでメモ。
環境
- Java : 1.8.0 Update 45
- IDE : IntelliJ IDEA 15 EAP
- Spring : 4.2.0.RELEASE
- AspectJ : 1.8.6
- Maven : 3.2.3
設定
こんな感じでpom.xml
にpluginを追加するだけ。
pom.xml
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
あと、必要に応じてライブラリを追加する。
- Spring AOP
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
- AspectJ
pom.xml
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>
あと、Spring Cacheを使っていると次のライブラリも必要。
pom.xml
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
番外編
環境が全て同じでも以下のエラーが出てはまっていた人がいたので、備忘録として追記。
エラーログ
$ mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for jp.ijufumi.sample:maven-sample:jar:0.0.1.SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-source-plugin is missing. @ line 579, column 21
[WARNING] The expression ${artifactId} is deprecated. Please use ${project.artifactId} instead.
[WARNING] The expression ${version} is deprecated. Please use ${project.version} instead.
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-sample 0.0.1.SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for org.codehaus.mojo:aspectj-maven-plugin:jar:1.8 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.437 s
[INFO] Finished at: 2016-05-19T18:57:23+09:00
[INFO] Final Memory: 5M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin org.codehaus.mojo:aspectj-maven-plugin:1.8 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.codehaus.
mojo:aspectj-maven-plugin:jar:1.8: 1 problem was encountered while building theeffective model for org.codehaus.mojo:aspectj-maven-plugin:1.8
[ERROR] [ERROR] 'dependencies.dependency.systemPath' for com.sun:tools:jar mustspecify an absolute path but is ${toolsjarSystemPath} @
[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/PluginResolutionException
$
このエラーが出た場合、内容通りtools.jar
がないってことで、JDK
入れたつもりだけどJRE入れてましたってことが
ググるとよく出てくる。
今回はJDK
入れているはずなのにってケースで、実はJDK
のインストール時にインストーラがデフォルトで指定したパスではなく別のパスを指定し、
JRE
のインストールの時にJDK
をインストールしたパスと同じパスを指定してしまったがために
JDK
をインストールしたつもりがJREだけをインストールした形になってしまっていた。
このことから、このようなエラーが発生する時は、
-
JDK
はちゃんとインストールしているか -
JAVA_HOME
、PATH
にJDK
をちゃんと指定しているか(IDEを使っている人はデフォルトのJDK
がJRE
になっていないか) -
JDK
のインストール時にミスってJRE
で上書きしていないか ← NEW
を確認する必要がある。
JDK
のインストール時にミスってJRE
で上書きしている場合は、javac
コマンドがエラーになったり、JAVA_HOME/lib
配下にtools.jar
がないので
どちらかが該当したらミスったと判断できる。