这是一个适合我的设置(使用记录不足的aspectj-maven-plugin)。
项目结构如下:
$ tree .
.
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── stackoverflow
│ └── Q3651690
│ ├── App.java
│ └── DontWriteToTheConsole.aj
└── test
└── java
└── com
└── stackoverflow
└── Q3651690
└── AppTest.java
具有以下小演示方面:
public aspect DontWriteToTheConsole {
pointcut sysOutOrErrAccess() : get(* System.out) || get(* System.err);
declare error
: sysOutOrErrAccess()
: "Don't write to the console";
}
pom.xml的配置如下:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow.Q3651690</groupId>
<artifactId>Q3651690</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Q3651690</name>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<goals>
<goal>compile</goal><!-- to weave all your main classes -->
<goal>test-compile</goal><!-- to weave all your test classes -->
</goals>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
关键部分是:
- 为 1.6 源代码级别配置 maven-compiler-plugin(这是使用
properties
)
- 为1.6源代码级别配置aspectj-maven-plugin(我在这里重用了用于配置maven-compiler-plugin)
properties
第二步似乎是多余的,但是,好吧,事情就是这样。
通过这种方式,我能够使用注释等编织代码:
$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3651690
[INFO] task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q3651690/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /home/pascal/Projects/stackoverflow/Q3651690/target/classes
[INFO] [aspectj:compile {execution: default}]
[ERROR] Don't write to the console
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Compiler errors:
error at System.out.println( "Hello World!" );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/home/pascal/Projects/stackoverflow/Q3651690/src/main/java/com/stackoverflow/Q3651690/App.java:11:0::0 Don't write to the console
see also: /home/pascal/Projects/stackoverflow/Q3651690/src/main/java/com/stackoverflow/Q3651690/DontWriteToTheConsole.aj:8::0
...