0

I meet some problems about the way of using aspectj-maven-plugin and aspectj.

First: I create ProjectA. It will be a 'compiled aspect' jar to intercept some functions of Logback and it is a public common jar so that other projects can use it.

I add below code in ProjectA's pom:



<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>xxx</version>
</dependency>


<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.14.0</version>
    <configuration>
        <showWeaveInfo>true</showWeaveInfo>
        <source>1.8</source>
        <target>1.8</target>
        <Xlint>ignore</Xlint>
        <complianceLevel>1.8</complianceLevel>
        <encoding>UTF-8</encoding>
        <verbose>true</verbose>
        <weaveDependencies>
            <weaveDependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </weaveDependency>
        </weaveDependencies>
    </configuration>
    <executions>
        <execution>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.7</version>
        </dependency>
    </dependencies>
</plugin>

And then I finish the specific intercept code like @Around("within(ch.qos.logback.classic.Logger+) After above, I package ProjectA as a jar.

Second: I create ProjectB. I apply ProjectA's jar in ProjectB,so I can intercept ProjectB' log produced by its logback with ProjectA.

I add ProjectA and in ProjectB' pom.xml

<dependency>
    <groupId>xxx</groupId>
    <artifactId>ProjectA</artifactId>
    <version>xxx</version>
</dependency>



<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.14.0</version>
    <configuration>
        <showWeaveInfo>true</showWeaveInfo>
        <source>1.8</source>
        <target>1.8</target>
        <Xlint>ignore</Xlint>
        <complianceLevel>1.8</complianceLevel>
        <encoding>UTF-8</encoding>
        <verbose>true</verbose>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>xxx</groupId>
                <artifactId>ProjectA</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.7</version>
        </dependency>
    </dependencies>
</plugin>


I package ProjectB with maven.I expect to see the wovend Logback code in ProjectB's output directory,but dont success.

Question During the process above,I refer to AspectJ: How to weave an aspect library into a Java project. I want to know

  1. is there something wrong with my idea?
  2. why I faild and the correct way to achieve my goal.

Pls help me~~~

Update on 11/1

I think I should redescribe the idea about the ProjectA. It is used by other project to track trace like Skywalking. The difference is that I choose Aspect CTW to realize the function. In ProjectA(a multi-module project), there are different module like mysql-module\logback-module....to intercept specific function by using @Aspect(execution) and then weave 'log' code. One module may have different VERSION like mysql-module-8.0 \ mysql-module-5.7 because the api function may change with version upgrading.

The other projects which will use ProjectA are based on Springboot. Now,I apply ProjectA in ProjectB,I code the ProjectB'pom.xml like above. If I directly use compiled ProjectA.jar in ProjectB ,it may cause VERSION CONFLICT. I dont want to use '' because there are many module in Project A and one module may have several VERSION.

So I want to try this way:

  1. I code the modules with @AspectJ in ProjectA.
  2. I have tried to solve the VERSION CONFLICT by develop aspectj-maven-plugin by judging ProjectA's and ProjectB's artifactId version.
  3. The most important thing is that I want to use aspectj-maven-plugin to compile and package in ProjectB . I expect that weaving by 'ProjectA’Class with @Aspect' happens during ProjectB's compile and package process. Because collegues used to run Springboot'Main Function directly in dev environment rather than deploy in a external Tomcat. By doing the 3 step,I can avoid VERSION CONFLICT in dev environment . And in proc_environment,I use a tomcat to deploy so that I can use aspect-maven-plugin to weave code.
Kim
  • 1
  • 1
  • Welcome to SO. I have an idea what might be wrong with your approach, but in order to know for sure, I would like to see an [MCVE](https://stackoverflow.com/help/mcve) project on GitHub which reproduces your problem. The reason is that several things could be wrong, e.g. wrong dependency management in a multi-module project. As soon as I see it, I probably can answer already. – kriegaex Oct 31 '22 at 15:44
  • @kriegaex Thanks for replying. I am sorry I cannot provide the code on github right now. I add some information about my question,Pls take a look. If you have any idea, pls tell me. I will try. Tks again. – Kim Nov 01 '22 at 08:40
  • Thanks for the additional information. But sorry, you are expecting too much, if you think that I will parse your prose and manually convert it into an [MCVE](https://stackoverflow.com/help/mcve) by myself to be able to reproduce your situation. Let your code speak! I do not need your original code, but a minimal version of the project with simple sample classes reproducing the situation. The devil is in the details, and textual descriptions are ambiguous. I want to help, so please do yourself a favour and help me to help you. If you are too busy for that, the question is not important enough. – kriegaex Nov 01 '22 at 08:50
  • @kriegaex Thanks for reading my poor English :) I will upload an MCVE after work. My company has a strict net limit and I have to edit the question on other mobile device not PC, that's why I say I cannot provide the code right now :) – Kim Nov 01 '22 at 09:07
  • Fine, I am in no hurry. My heartfelt condolences for your company's weird policy. – kriegaex Nov 01 '22 at 09:16
  • You ignored this question - **your** question - for 3+ weeks. Please either delete it altogether or enhance it by means of an MCVE. Thank you. – kriegaex Nov 24 '22 at 07:07

0 Answers0