19

java 6.0 を使用して、aspectj を Maven プロジェクトに追加しようとしています。ブラウジングすると、2 つの Maven プラグインが見つかりましたが、どれも期待どおりに動作しません。

最初のものhttp://mojo.codehaus.org/aspectj-maven-pluginは、5.0以降のソースをコンパイルするコードを取得できなかったため、最初はnetbeansを介して機能しませんでした(注釈などについて不平を言いました)コマンドから試した後機能した行と実行されたコマンドを比較すると、コンパイルの目標は正常に機能しますが、プラグインおよびJava 5+コードと互換性のないmavensのインストールの目標のようです。これを回避することは可能かもしれませんが、それは煩わしいので、次の質問に行き着きます。aspectj-maven-plugin はまだ開発中ですか? 私はまだそれを使用する必要がありますか?

2 つ目は apaches のもので、よりアクティブで動作する可能性が高いようです。ただし、完全な例が見つからず、実行できません。Mavenから例外が発生し続けます:

java.lang.IllegalStateException: The plugin descriptor for the plugin Plugin [maven:maven-aspectj-plugin] was not found. Please verify that the plugin JAR /home/kristofer/.m2/repository/maven/maven-aspectj-plugin/4.0/maven-aspectj-plugin-4.0.jar is intact.

jarファイルはそのままで、使用するプラグインのバージョンに関係なく、常に同じ例外がスローされます。問題が何であるかについてのアイデアはありますか?

要するに、どのプラグインをどのように使用すればよいですか?

ありがとう

4

3 に答える 3

17

これは私のために機能するセットアップです(以下に記載されているspectj-maven-pluginを使用)。

プロジェクトの構造は次のとおりです。

$ツリー。
.
├── pom.xml
└── src
    ├──メイン
    │ └──ジャワ
    │ └──com
    │ └── スタックオーバーフロー
    │ └── Q3651690
    │ ├── App.java
    │ └── DontWriteToTheConsole.aj
    └──テスト
        └──ジャワ
            └──コム
                └──スタックオーバーフロー
                    └──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を設定します(propertiesここでmaven-compiler-pluginを設定するために使用したものを再利用しました)

2 番目のステップは冗長に思えますが、まあ、そういうことです。

このようにして、注釈などを使用してコードを織り込むことができました。

$ mvn クリーン インストール
[情報] プロジェクトをスキャンしています...
[情報]  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - - -------------------------
[INFO] 建物 Q3651690
[情報] タスク セグメント: [クリーン、インストール]
[情報]  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - - -------------------------
[情報] [クリーン: クリーン {実行: デフォルト-クリーン}]
[INFO] [resources:resources {execution: default-resources}]
[情報] 'UTF-8' エンコーディングを使用して、フィルター処理されたリソースをコピーしています。
[INFO] 存在しない resourceDirectory をスキップします /home/pascal/Projects/stackoverflow/Q3651690/src/main/resources
[情報] [コンパイラ: コンパイル {実行: デフォルト コンパイル}]
[情報] 1 つのソース ファイルを /home/pascal/Projects/stackoverflow/Q3651690/target/classes にコンパイルしています
[INFO] [aspectj:compile {実行: デフォルト}]
[エラー] コンソールに書き込まないでください
[情報]  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - - -------------------------
[エラー] ビルド エラー
[情報]  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - - -------------------------
[情報] コンパイラ エラー:
System.out.println( "Hello World!" ) でエラーが発生しました。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/home/pascal/Projects/stackoverflow/Q3651690/src/main/java/com/stackoverflow/Q3651690/App.java:11:0::0 コンソールに書き込まない
    参照: /home/pascal/Projects/stackoverflow/Q3651690/src/main/java/com/stackoverflow/Q3651690/DontWriteToTheConsole.aj:8::0
...
于 2010-09-07T02:09:11.720 に答える
3

Maven Compiler プラグインを使用して、AspectJ を使用するようにコンパイラを変更できます。

構成は次のようになります。

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <compilerId>aspectj</compilerId>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-aspectj</artifactId>
            <version>1.6</version>
        </dependency>
    </dependencies>
</plugin>

資力 :

同じトピックで:

于 2010-09-06T13:17:02.517 に答える
2

we're using the aspectj-maven-plugin for building several large production grade J2EE systems. Lately, the development on that plugin doesn't seem to be overly active. The last relase has been last winter, and there are some serious problems with "AspectLib" and "WeaveDependencies", which got reported several times (even with attached bugfixes), without any responses from upstream.

But anyway, the basic functionality is working, and this plugin supports lots of configuration needed in real world projects.

Pascal Thivent showed in his (very good) response above how to configure the plugin with a special dependency section. You can use this trick also to configure the actual AspectJ version used for compilation, as the version used by default by this plugin is somewhat dated....

<project xmlns=....

<properties>
    <aspectjVer>1.6.9</aspectjVer>
    ....
    ....
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
....

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.3</version>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectjVer}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectjVer}</version>
                </dependency>
            </dependencies>
            <configuration>
       ....
</build>
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectjVer}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId><!-- only needed if you use Spring-AOP -->
        <version>${aspectjVer}</version>
    </dependency>
    ....
    ....

Note the fact that the plugin has an classpath environment which is independent from your project's classpath. Thus we have to add the AspectJ-Runtime explicitly to the project dependencies.

于 2010-09-12T00:15:47.760 に答える