AspectJ Maven 插件破坏了 GMaven 构建的 Groovy 类
我正在尝试在模块上运行 Groovy 和 AspectJ,但是当我添加 AspectJ 时,这些类似乎返回与我预期非常不同的值:
junit.framework.ComparisonFailure: null expected:<2011-04-03> but was:<null>
testGetUnixDayFromDate(com.baselogic.chapter05.utils.DateUtilitiesTest) Time elapsed: 0.016 sec <<< FAILURE!
junit.framework.AssertionFailedError: expected:<0> but was:<15037>
testGetDateDifferenceInDays(com.baselogic.chapter05.utils.DateUtilitiesTest) Time elapsed: 0 sec <<< FAILURE!
junit.framework.AssertionFailedError: expected:<0> but was:<7>
这不是我预期的,当我关闭 AspectJ 时,这一切都运行良好。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.baselogic.chapter05.utils.DateUtilitiesJavaTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.422 sec
Running com.baselogic.chapter05.utils.DateUtilitiesTest
2011-04-03
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec
Running com.baselogic.chapter05.utils.StringUtilitiesTest
Tests run: 26, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec
Results :
Tests run: 30, Failures: 0, Errors: 0, Skipped: 0
...
这是我的 AspectJ 插件,它似乎导致了问题:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<complianceLevel>1.6</complianceLevel>
</configuration>
</plugin>
但是当我启用 aspectj-maven-plugin 时,一切都开始失败:
[INFO] ------------------------------------------------------------------------
[INFO] Building Chapter 05: Extending 1.0.2
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ ch05 ---
[INFO] Deleting C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target
[INFO]
[INFO] --- gmaven-plugin:1.3:generateStubs (default) @ ch05 ---
[INFO] Generated 2 Java stubs
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ ch05 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ ch05 ---
[INFO] Compiling 2 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\classes
[INFO]
[INFO] --- maven-scala-plugin:2.15.2:compile (default) @ ch05 ---
[INFO] Checking for multiple versions of scala
[INFO] includes = [**/*.scala,**/*.java,]
[INFO] excludes = []
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\java:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\generated-sources\groovy-stubs\main:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\groovy:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\scala:-1: info: compiling
[INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\classes at 1301955015855
[INFO] prepare-compile in 0 s
[INFO] compile in 4 s
[INFO]
[INFO] --- gmaven-plugin:1.3:compile (default) @ ch05 ---
[INFO] Compiled 3 Groovy classes
[INFO]
[INFO] --- clojure-maven-plugin:1.3.7:compile (compile-clojure) @ ch05 ---
[INFO]
[INFO] --- aspectj-maven-plugin:1.3.1:compile (default) @ ch05 ---
[INFO]
[INFO] --- gmaven-plugin:1.3:generateTestStubs (default) @ ch05 ---
[INFO] Generated 2 Java stubs
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ ch05 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ ch05 ---
[INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\test-classes
[INFO]
[INFO] --- maven-scala-plugin:2.15.2:testCompile (default) @ ch05 ---
[INFO] Checking for multiple versions of scala
[INFO] includes = [**/*.scala,**/*.java,]
[INFO] excludes = []
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\java:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\generated-sources\groovy-stubs\test:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\groovy:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\scala:-1: info: compiling
[INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\test-classes at 1301955027323
[INFO] prepare-compile in 0 s
[INFO] compile in 3 s
[INFO]
[INFO] --- gmaven-plugin:1.3:testCompile (default) @ ch05 ---
[INFO] Compiled 2 Groovy classes
[INFO]
[INFO] --- aspectj-maven-plugin:1.3.1:test-compile (default) @ ch05 ---
[WARNING] No sources found skipping aspectJ compile
[INFO]
[INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ ch05 ---
[INFO] Surefire report directory: C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.baselogic.chapter05.utils.DateUtilitiesJavaTest
Apr 4, 2011 6:10:31 PM com.baselogic.chapter05.utils.DateUtilities getYesterdayDate_aroundBody11$advice
INFO: --------------------
Apr 4, 2011 6:10:31 PM com.baselogic.chapter05.utils.DateUtilities getYesterdayDate_aroundBody11$advice
...
Tests run: 26, Failures: 21, Errors: 0, Skipped: 0, Time elapsed: 0.093 sec <<< FAILURE!
Results :
Failed tests:
testGetYesterdayDate(com.baselogic.chapter05.utils.DateUtilitiesJavaTest)
...
Tests run: 31, Failures: 26, Errors: 0, Skipped: 0
[ERROR] There are test failures.
Please refer to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\surefire-reports for the individual test results.
这是 Groovy 测试:
package com.baselogic.chapter05.utils
import groovy.util.GroovyTestCase
import java.util.Calendar
import java.text.SimpleDateFormat
class DateUtilitiesTest extends GroovyTestCase {
protected void setUp() {
super.setUp()
}
protected void tearDown() {
super.tearDown()
}
public void testGetYesterdayDate() {
Calendar calendar = Calendar.getInstance()
calendar.add(Calendar.DATE, -1)
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd")
String expected = format.format(calendar.getTime());
String result = DateUtilities.getYesterdayDate()
println(result)
assertEquals(expected, result)
}
void testGetUnixDayFromDate() {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date = (java.util.Date) formatter.parse("2011-03-03 23:59:59");
assertEquals(DateUtilities.getUnixDayFromDate(date), 15037);
}
void testGetDateDifferenceInDays() {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date1 = (java.util.Date) formatter.parse("2011-03-03 23:59:59");
java.util.Date date2 = (java.util.Date) formatter.parse("2011-03-10 23:59:59");
assertEquals(DateUtilities.getDateDifferenceInDays(date1, date2), 7);
}
}
然后我还创建了一个 Java 单元测试用例:
package com.baselogic.chapter05.utils;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateUtilitiesJavaTest {
@Test
public void testGetYesterdayDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String expected = format.format(calendar.getTime());
String result = DateUtilities.getYesterdayDate();
// Aspect DontWriteToTheConsole will complain:
// System.out.println(result);
assertEquals(expected, result);
}
@Test
public void testGetYesterdayDateScala() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String expected = format.format(calendar.getTime());
String result = ScalaDateUtilities.getYesterdayDate();
assertEquals(expected, result);
}
}
谁能帮助我理解为什么 aspectj-maven-plugin 似乎会导致我的最终字节码出现编译错误?
I am trying to run Groovy and AspectJ on a module but when I add AspectJ, the classes seem to return very diferent values than I expected:
junit.framework.ComparisonFailure: null expected:<2011-04-03> but was:<null>
testGetUnixDayFromDate(com.baselogic.chapter05.utils.DateUtilitiesTest) Time elapsed: 0.016 sec <<< FAILURE!
junit.framework.AssertionFailedError: expected:<0> but was:<15037>
testGetDateDifferenceInDays(com.baselogic.chapter05.utils.DateUtilitiesTest) Time elapsed: 0 sec <<< FAILURE!
junit.framework.AssertionFailedError: expected:<0> but was:<7>
This is not what I expected and when I turn the off AspectJ, this all runs fine.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.baselogic.chapter05.utils.DateUtilitiesJavaTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.422 sec
Running com.baselogic.chapter05.utils.DateUtilitiesTest
2011-04-03
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec
Running com.baselogic.chapter05.utils.StringUtilitiesTest
Tests run: 26, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec
Results :
Tests run: 30, Failures: 0, Errors: 0, Skipped: 0
...
Here is my AspectJ plugin that seems to be causing the issue:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<complianceLevel>1.6</complianceLevel>
</configuration>
</plugin>
But when I enable aspectj-maven-plugin everything starts failing:
[INFO] ------------------------------------------------------------------------
[INFO] Building Chapter 05: Extending 1.0.2
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ ch05 ---
[INFO] Deleting C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target
[INFO]
[INFO] --- gmaven-plugin:1.3:generateStubs (default) @ ch05 ---
[INFO] Generated 2 Java stubs
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ ch05 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ ch05 ---
[INFO] Compiling 2 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\classes
[INFO]
[INFO] --- maven-scala-plugin:2.15.2:compile (default) @ ch05 ---
[INFO] Checking for multiple versions of scala
[INFO] includes = [**/*.scala,**/*.java,]
[INFO] excludes = []
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\java:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\generated-sources\groovy-stubs\main:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\groovy:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\scala:-1: info: compiling
[INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\classes at 1301955015855
[INFO] prepare-compile in 0 s
[INFO] compile in 4 s
[INFO]
[INFO] --- gmaven-plugin:1.3:compile (default) @ ch05 ---
[INFO] Compiled 3 Groovy classes
[INFO]
[INFO] --- clojure-maven-plugin:1.3.7:compile (compile-clojure) @ ch05 ---
[INFO]
[INFO] --- aspectj-maven-plugin:1.3.1:compile (default) @ ch05 ---
[INFO]
[INFO] --- gmaven-plugin:1.3:generateTestStubs (default) @ ch05 ---
[INFO] Generated 2 Java stubs
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ ch05 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ ch05 ---
[INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\test-classes
[INFO]
[INFO] --- maven-scala-plugin:2.15.2:testCompile (default) @ ch05 ---
[INFO] Checking for multiple versions of scala
[INFO] includes = [**/*.scala,**/*.java,]
[INFO] excludes = []
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\java:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\generated-sources\groovy-stubs\test:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\groovy:-1: info: compiling
[INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\scala:-1: info: compiling
[INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\test-classes at 1301955027323
[INFO] prepare-compile in 0 s
[INFO] compile in 3 s
[INFO]
[INFO] --- gmaven-plugin:1.3:testCompile (default) @ ch05 ---
[INFO] Compiled 2 Groovy classes
[INFO]
[INFO] --- aspectj-maven-plugin:1.3.1:test-compile (default) @ ch05 ---
[WARNING] No sources found skipping aspectJ compile
[INFO]
[INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ ch05 ---
[INFO] Surefire report directory: C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.baselogic.chapter05.utils.DateUtilitiesJavaTest
Apr 4, 2011 6:10:31 PM com.baselogic.chapter05.utils.DateUtilities getYesterdayDate_aroundBody11$advice
INFO: --------------------
Apr 4, 2011 6:10:31 PM com.baselogic.chapter05.utils.DateUtilities getYesterdayDate_aroundBody11$advice
...
Tests run: 26, Failures: 21, Errors: 0, Skipped: 0, Time elapsed: 0.093 sec <<< FAILURE!
Results :
Failed tests:
testGetYesterdayDate(com.baselogic.chapter05.utils.DateUtilitiesJavaTest)
...
Tests run: 31, Failures: 26, Errors: 0, Skipped: 0
[ERROR] There are test failures.
Please refer to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\surefire-reports for the individual test results.
Here is the Groovy test:
package com.baselogic.chapter05.utils
import groovy.util.GroovyTestCase
import java.util.Calendar
import java.text.SimpleDateFormat
class DateUtilitiesTest extends GroovyTestCase {
protected void setUp() {
super.setUp()
}
protected void tearDown() {
super.tearDown()
}
public void testGetYesterdayDate() {
Calendar calendar = Calendar.getInstance()
calendar.add(Calendar.DATE, -1)
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd")
String expected = format.format(calendar.getTime());
String result = DateUtilities.getYesterdayDate()
println(result)
assertEquals(expected, result)
}
void testGetUnixDayFromDate() {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date = (java.util.Date) formatter.parse("2011-03-03 23:59:59");
assertEquals(DateUtilities.getUnixDayFromDate(date), 15037);
}
void testGetDateDifferenceInDays() {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date1 = (java.util.Date) formatter.parse("2011-03-03 23:59:59");
java.util.Date date2 = (java.util.Date) formatter.parse("2011-03-10 23:59:59");
assertEquals(DateUtilities.getDateDifferenceInDays(date1, date2), 7);
}
}
Then I created a Java unit test case as well:
package com.baselogic.chapter05.utils;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateUtilitiesJavaTest {
@Test
public void testGetYesterdayDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String expected = format.format(calendar.getTime());
String result = DateUtilities.getYesterdayDate();
// Aspect DontWriteToTheConsole will complain:
// System.out.println(result);
assertEquals(expected, result);
}
@Test
public void testGetYesterdayDateScala() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String expected = format.format(calendar.getTime());
String result = ScalaDateUtilities.getYesterdayDate();
assertEquals(expected, result);
}
}
Can anyone help me understand why the aspectj-maven-plugin seems to be causing compilation errors with my final bytecode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 GMaven 的体验很差。一旦我切换到 Gradle,很多“怪异”就消失了。这听起来有点奇怪,你不想在 GMaven 上浪费时间。
My experience with the GMaven has been poor. Once I switched to Gradle, much of the 'weirdness' went away. This sounds like that kind of weirdness you don't want to waste time with GMaven.
我认为切换到 Gradle 不是一个解决方案,而是一个昂贵的解决方法,尤其是对于大型项目。所以我不同意Mike Wazowski的观点。也许你应该修复你的 Maven 设置。您可以执行以下操作:
首先,AspectJ Maven 1.3.1 是 2011 年发布的,并且使用非常旧的 AspectJ 版本 (1.6.10)。为什么不升级到默认使用 AsepctJ 1.8.2 的 AspectJ Maven 1.7?您甚至可以将其升级到 AspectJ 1.8.6,这是包含更多错误修复的最新版本?
您还可以考虑从 GMaven 切换到 GMavenPlus,因为据我所知,前者不再维护,而后者是。
顺便说一句,您还可以从 Java 6 切换到 8 或至少 7,因为 Java 6 不再维护。
我的 AspectJ Maven 设置通常看起来像这样(我在这里省略了 GMaven 设置):
I think that switching to Gradle is not a solution but an expensive workaround, especially for big projects. So I disagree with Mike Wazowski. Maybe you should just fix your Maven setup. There are several things you can do:
First of all, AspectJ Maven 1.3.1 ist from 2011 and uses a very old AspectJ version (1.6.10). Why not upgrade to AspectJ Maven 1.7 which uses AsepctJ 1.8.2 by default? You can even upgrade it to AspectJ 1.8.6, the latest version containing more bugfixes?
You could also consider switching from GMaven to GMavenPlus because the former is no longer mainained AFAIK, while the latter is.
BTW, you could also switch from Java 6 to 8 or at least 7 because Java 6 is no longer maintained.
My AspectJ Maven setup usually looks something like this (I am omitting GMaven setup here):