0

I use Eclipse Kepler, Java 1.7. Part of my pom.xml is below. As I see during maven compile, no logs about weaving at all. Neither I have any errors. Aspect doesn't work neither. What am I doing wrong? As I see in some examples, this pom should work. I installed AspectJ tools to Eclipse.

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <source>${compiler.version}</source>
                <target>${compiler.version}</target>
                <Xlint>ignore</Xlint>
                <complianceLevel>${compiler.version}</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>true</verbose>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
            </plugin>

Edited: This is my aspect code:

package util;

import java.util.logging.Logger;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.StopWatch;

@Aspect
public class MyAspect {
    private static final Logger logger = Logger.getLogger(MyAspect.class.getName());

    @Around("execution(public * filter.RolesFilter.doFilter(..))")
    public Object loggingMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        Object retVal = joinPoint.proceed();

        stopWatch.stop();

        StringBuffer logMessage = new StringBuffer();
        logMessage.append(joinPoint.getTarget().getClass().getName());
        logMessage.append(".");
        logMessage.append(joinPoint.getSignature().getName());
        logMessage.append("(");
        // append args
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            logMessage.append(args[i]).append(",");
        }
        if (args.length > 0) {
            logMessage.deleteCharAt(logMessage.length() - 1);
        }
        logMessage.append(")");
        logMessage.append(" execution time: ");
        logMessage.append(stopWatch.getTotalTimeMillis());
        logMessage.append(" ms");
        System.out.println(logMessage.toString());
        logger.info(logMessage.toString());
        return retVal;
    }

}

The method I want to weave with my aspect:

package filter;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RolesFilter implements Filter {
    Logger logger = Logger.getLogger(RolesFilter.class.getName());

    public RolesFilter() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException {
        logger.info("This is logger message");
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        String reqURI = req.getRequestURI();
        String userName = (String) session.getAttribute("username");
        Character role = (Character) session.getAttribute("role");
        try {
            if (role != null && role.charValue() == 'A') {
                if (reqURI.indexOf("/admin/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/view/roles/admin/home.xhtml");
                }
            } else if (role != null && role.charValue() == 'B') {
                if (reqURI.indexOf("/biller/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/biller.xhtml");
                }
            } else if (role != null && role.charValue() == 'C') {
                if (reqURI.indexOf("/customer/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/customer.xhtml");
                }
            } else {
                res.sendRedirect(req.getContextPath()
                        + "/login.xhtml");
            }
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Also, here is maven clean compile output:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for myproject:war:0.0.1-SNAPSHOT
[WARNING] The expression ${build.sourceDirectory} is deprecated. Please use ${project.build.sourceDirectory} instead.
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ myproject ---
[INFO] Deleting /Users/user1/git/myproject/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.1:compile (default-compile) @ myproject ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 41 source files to /Users/user1/git/myproject/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.007 s
[INFO] Finished at: 2015-02-16T10:00:23-08:00
[INFO] Final Memory: 12M/245M
[INFO] ------------------------------------------------------------------------
Battle_Slug
  • 2,055
  • 1
  • 34
  • 60
  • 1
    Thanks for sharing a part of your Maven configuration, but where is the application and aspect code? Where is the Maven console output? It is pretty hard to say anything intelligent about your problem without that information. Please update your question, ideally create and share a minimal [SSCCE](http://sscce.org/) reproducing the problem, maybe on GitHub. – kriegaex Feb 09 '15 at 20:13
  • 1
    Please post the `RolesFilter` class, too (including package declaration) and also the output of `mvn clean compile`. Oh, and BTW: If you use native AspectJ inside Spring, is it on purpose or do you actually want to use Spring AOP? In the latter case, the aspect should also be a Spring `@Component`, AFAIK. – kriegaex Feb 14 '15 at 12:08
  • @kriegaex Thank you for participating. I added both. Regarding AspectJ and Spring AOP - I just try to follow different tutorials. I failed to make it work though I tried everything I found. Something is wrong, but I cannot get what. – Battle_Slug Feb 16 '15 at 18:04
  • Your AspectJ Maven Plugin does not run. Have you just put it into a `` section but forgot to also add an actual `` section activating it? The Maven output suggests it and you have only posted a snippet from your POM, so it is hard to tell. – kriegaex Feb 16 '15 at 23:01
  • @kriegaex All plugins are inside the tag, which itself is inside the tag. It's just too long to post here... – Battle_Slug Feb 16 '15 at 23:34
  • 1
    Whatever is in `pluginManagement` is just preconfiguration. You need to also have an additional stand-alone `plugins` section *outside* of `pluginManagement` where you declare which of the preconfigured plugins (without version number and configuration, just group and artifact IDs) you actually want to use during the build. This is Maven basics. – kriegaex Mar 15 '15 at 18:05

2 Answers2

3

Your Maven log shows no sign of aspectj-maven-plugin being executed at all, consequently there is no weaving. I suppose you have put your plugin config into the <pluginManagement> section, but have not referenced the configured plugin from the <plugins> section. This would explain the missing plugin output. I.e. you need to do this:

<pluginManagement>
    <plugins>
        <!-- Configure plugin here -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                (...)
            </configuration>
            (...)
    </plugins>
</pluginManagement>

(...)

<plugins>
    <!-- Refer to the pre-configured plugin here in order to actually use it -->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
    </plugin>
</plugins>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
0

Have you created a aop.xml and placed under the Meta-inf folder and in that aop.xml have you placed the package details of the classes that needs to be weaved ?

Vaidy
  • 19
  • 4
  • I used annotation in my aspect class: @Around("execution(public filter.RolesFilter.doFilter(..))") – Battle_Slug Feb 09 '15 at 16:48
  • 1
    @Vaidy: Your answer does not make sense. The question is about compile-time weaving, but *aop.xml* is only used for load-time weaving. – kriegaex Feb 09 '15 at 20:10
  • @Battle_Slug: The pointcut syntax of your `@Around` advice is wrong. You do not specify a method return type. So you should see an AspectJ compiler error. For example, this syntax would be correct: `@Around("execution(public * filter.RolesFilter.doFilter(..))")` - please note the extra `*`. There still might be a problem with package names or so, I cannot say without seeing your code. – kriegaex Feb 09 '15 at 20:16
  • @kriegaex I added the code of my aspect. Actually, I have no errors at all. Like there is no AspectJ working in my project. – Battle_Slug Feb 10 '15 at 00:46