0

I have been dealing with the problem for a day. I have a quite simple project so far, but still not able to configure AspectJ

My project structure looks as follows.

-- root   
---- DesktopGUI  
---- DataStore  
---- localsettings  
---- crosscuttingconcerns  

I have defined all AspectJ stuff in the cross-cutting-concerns module.

My root pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <properties>
        <example.desktop.version>1.0-SNAPSHOT</example.desktop.version>
        <example.desktop.settings.version>1.0-SNAPSHOT</example.desktop.settings.version>
        <example.desktop.gui.version>1.0-SNAPSHOT</example.desktop.gui.version>
        <aspectj.version>1.8.13</aspectj.version>
    </properties>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.solutions</groupId>
    <artifactId>example</artifactId>
    <packaging>pom</packaging>
    <version>${example.desktop.version}</version>

    <modules>
        <module>DesktopGUI</module>
        <module>DataStore</module>
        <module>localsettings</module>
        <module>crosscuttingconcerns</module>
    </modules>

</project>

DesktopGUI pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>example</artifactId>
        <groupId>com.example.solutions</groupId>
        <version>${example.desktop.version}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>desktop-gui</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <mvvmfx.version>1.7.0</mvvmfx.version>
        <jfoenix.version>8.0.1</jfoenix.version>
        <jfxtrascontrols.version>8.0-r5</jfxtrascontrols.version>
        <fontawesomefx.version>8.9</fontawesomefx.version>
        <guice.version>4.1.0</guice.version>
    </properties>
    <!--Dependencies-->
    <dependencies>
        <!--DI, IoC Frameworks Dependencies-->
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-grapher</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>de.saxsys</groupId>
            <artifactId>mvvmfx</artifactId>
            <version>${mvvmfx.version}</version>
        </dependency>
        <!--UI Dependencies-->
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>${jfoenix.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jfxtras</groupId>
            <artifactId>jfxtras-controls</artifactId>
            <version>${jfxtrascontrols.version}</version>
        </dependency>
        <dependency>
            <groupId>de.jensd</groupId>
            <artifactId>fontawesomefx</artifactId>
            <version>${fontawesomefx.version}</version>
        </dependency>
        <dependency>
            <groupId>de.saxsys</groupId>
            <artifactId>mvvmfx-guice</artifactId>
            <version>${mvvmfx.version}</version>
        </dependency>

        <!--Local project dependencies-->
        <dependency>
            <groupId>com.example.solutions</groupId>
            <artifactId>local-settings</artifactId>
            <version>${example.desktop.settings.version}</version>
        </dependency>
        <dependency>
            <groupId>com.example.solutions</groupId>
            <artifactId>cross-cutting-concerns</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project> 

Cross-Cutting-Concerns pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>sto</artifactId>
        <groupId>com.example.solutions</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <properties>
        <aspectj.version>1.8.13</aspectj.version>
        <slf4f.version>1.8.0-beta0</slf4f.version>
    </properties>
    <artifactId>cross-cutting-concerns</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4f.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4f.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>

    </dependencies>
</project>

I have defined my aspect as follows

@Aspect
public class MethodLogger {
    private static Logger logger = LoggerFactory.getLogger(MethodLogger.class);

    @Pointcut("execution(* *(..)) && @annotation(com.example.solutions.example.crosscuttingconcerns.annotation.LogMethodCall)")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) {
        try {
            long start = System.currentTimeMillis();
            Object result = point.proceed();
            System.out.println("HELLOO");
            logger.info("#%s(%s): %s in %[msec]s",
                    MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
                    point.getArgs(),
                    result,
                    System.currentTimeMillis() - start
            );
            return result;
        } catch (Throwable ex) {
            return null;
        }
    }
}

And annotated my method like that

@Override
@LogMethodCall
public void startMvvmfx(Stage stage) throws Exception {
    stage.setTitle(internalizationManager.getStringValue("titles", "main_app"));
    stage.show();
    stage.centerOnScreen();
}

This method is defined in the DesktopGUI module

Annotation defined like that

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogMethodCall {
}

It compiles without errors, but with a warning

Warning:(21, 0) ajc: advice defined in com.example.solutions.example.crosscuttingconcerns.aspect.MethodLogger has not been applied [Xlint:adviceDidNotMatch]

I have tried to add ApsectJ facets to my modules, but this still didn't help.

I think that the problem may be caused by multi module project structure.

Please help to solve this problem, I have not ideas what is wrong.

I have set Java Compiler to Ajc as well

enter image description here

Thanks

kriegaex
  • 63,017
  • 15
  • 111
  • 202

1 Answers1

0

I see no trace of AspectJ Maven plugin in your POMs, i.e. the AspectJ compiler is not used and thus you will never be able to use AOP in your project. You need the plugin in both modules, assuming that you want to use compile-time weaving. In the aspect library module you need it in order to compile the aspects properly and in the other module(s) you need it in order to weave the aspects into your core code, referring to the aspect library.

For a more complex multi-module setup see here, but IMO you do not need that for your simple project.

See my other answer for a complete sample project. Both IDEA with AspectJ extension installed and Eclipse with AJDT installed import that Maven setup flawlessly and are able to build and run the project from both Maven and the IDE.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Thanks for the answer, I have set java compiler to ajc, should I also use the AspectJ maven plugin ? –  Jan 31 '18 at 09:50
  • Not also, but instead. So please do not change Maven Compiler plugin settings but rather use AspectJ Maven plugin instead. That is what it was made for. Ajc has so many special command line settings, it would be quite tedious and not comfortable at all to configure them via Maven Compiler (if possible at all). – kriegaex Jan 31 '18 at 12:28