Announcement Announcement Module
Collapse
No announcement yet.
Any body knows how to make @Configurable work with Maven? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Any body knows how to make @Configurable work with Maven?

    The project I'm doing now uses @Configurable to inject domain objects, and it has got an Ant script to weave spring aspects.

    Now we decide to move on to Maven, but I could not get Maven aspectj plugin to work, has any body tried to use maven aspectj plugin to weave @Configurable? It would be nice if you could post your pom.xml.

    Here's my pom
    Code:
    <build>
    	<finalName>transcriptexpress</finalName>
    	<plugins>
    		<plugin>
    			<artifactId>maven-compiler-plugin</artifactId>
    			<configuration>
    				<source>1.5</source>
    				<target>1.5</target>
    			</configuration>
    		</plugin>
    	
    		<plugin>
    			<groupId>org.codehaus.mojo</groupId>
    			<artifactId>aspectj-maven-plugin</artifactId>
    				
    			<configuration>
    				<verbose>true</verbose>
    				<complianceLevel>1.5</complianceLevel>
    				<source>1.5</source>
    				<showWeaveInfo>true</showWeaveInfo>
    				<outxml>true</outxml>
    				<aspectLibraries>
    					<aspectLibrary>
    						<groupId>org.springframework</groupId>
    						<artifactId>spring-aspects</artifactId>
    					</aspectLibrary>
    				</aspectLibraries>
    			</configuration>
    				
    			<executions>
    				<execution>
    					<goals>
    						<goal>compile</goal>       <!-- use this goal to weave all your main classes -->
    					</goals>
    				</execution>
    			</executions>
    		</plugin>
    	</plugins>
      ....
    </build>
    I did include aspectj related libraries in the dependency, and I'm using 1.5.2a version.

    When I run 'mvn compile', I can see ajc compiles my java source but it doesn't weave at all, can't see weaving info.

    Can anybody see something wrong within my pom?

  • #2
    Can you find the actual call made to ajc by Maven?

    Comment


    • #3
      No

      Sorry, can't see ajc calls,
      The only thing I can see is ajc compiler compiles all the classes, without weaving

      Comment


      • #4
        I just tried, and it worked the first time. Amazing :-) I've not been using @Configurable before because of the java-agent that I thought was required, didn't realize that it was this simple to get it working without the agent...

        I just added a test to my existing setup. Posting the parts that may be relevant.

        In my pom.xml inside build and <plugins> I have this:

        Code:
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>aspectj-maven-plugin</artifactId>
                        
                        <configuration>
                            <verbose>true</verbose>
                            <complianceLevel>1.5</complianceLevel>
                            <source>1.5</source>
                            <showWeaveInfo>true</showWeaveInfo>
                            <outxml>true</outxml>
                            <aspectLibraries>
                                <aspectLibrary>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-aspects</artifactId>
                                </aspectLibrary>
                            </aspectLibraries>
                            <includes>
                                <include>**/Property.java</include>
                            </includes>
                        </configuration>
                        
                        <executions>
                            <execution>
                                <goals>
                                    <goal>compile</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
        Inside pom.xml <dependencies> I have this:
        Code:
        <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                    <version>2.0</version>
                </dependency>
        My domain object (Also mapped by hibernate JPA, but ignore that part. It's @Configurable and the message String with setter thats important):

        Code:
        package se.lantmateriet.elips.poc.domain.model;
        
        import java.util.HashSet;
        import java.util.Set;
        import java.util.UUID;
        
        import javax.persistence.CascadeType;
        import javax.persistence.Entity;
        import javax.persistence.OneToMany;
        import javax.persistence.Transient;
        
        import org.springframework.beans.factory.annotation.Configurable;
        
        @Configurable
        @Entity
        public class Property extends AbstractDomainEntity {
        
            @Transient
            private String message;
            
            @OneToMany(mappedBy="property", cascade=CascadeType.ALL)
            private Set<Parcel> parcels;
            
            public Property(UUID objectId, Integer objectVersion) {
                super(objectId, objectVersion);
                this.parcels = new HashSet<Parcel>();
            }
        
            private Property() {
                super();
            }
                
            public void addParcel(Parcel parcel) {
                parcel.setProperty(this);
                this.parcels.add(parcel);
            }
            
            public void removeParcel(Parcel parcel) {
                this.parcels.remove(parcel);
            }
        
            public Set<Parcel> getParcels() {
                return this.parcels;
            }
        
            public String getMessage() {
                return this.message;
            }
        
            public void setMessage(String message) {
                this.message = message;
            }
            
        }
        Spring config:

        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:util="http://www.springframework.org/schema/util"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                 http://www.springframework.org/schema/util
               http://www.springframework.org/schema/util/spring-util-2.0.xsd"
            default-autowire="no">
            
            <aop:spring-configured/>
            
            <bean id="helloWorld" class="java.lang.String">
                <constructor-arg>
                    <value>Hello World</value>
                </constructor-arg>
            </bean>
            
            <bean class="se.lantmateriet.elips.poc.domain.model.Property" scope="property">
                <property name="message" ref="helloWorld"/>
            </bean>
        </beans>
        And testcase:

        Code:
        package se.lantmateriet.elips.poc.domain.model;
        
        import java.util.UUID;
        
        import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
        
        public class DomainInjectionTest extends
                AbstractDependencyInjectionSpringContextTests {
        
            @Override
            protected String[] getConfigLocations() {
                return new String[] { "classpath*:META-INF/spring-config/**/domain.xml" };
            }
        
            public void testInjection() {
                Property property = new Property(UUID.randomUUID(), 0);
                
                assertEquals("Hello World", property.getMessage());
                logger.debug(property.getMessage());
            }
            
        
        }
        Hope this helps. No output is made from the weaving, but it does weave.

        /Magnus Heino

        Comment


        • #5
          Re: Any body knows how to make @Configurable work with Maven?

          I have been having the same trouble as you. I didn't think any weaving was going on. However, run mvn with the -X option turned on (debug output) and you'll see that your stuff is being advised (I had to pipe the output to a file and analyze it that way).

          Comment


          • #6
            Originally posted by magnusheino View Post
            Hope this helps. No output is made from the weaving, but it does weave.
            Hi!

            Thank you for posting this code!

            I have you tried to run the test from eclipse (with ajdt)?

            I ask since I have some problems with getting runtime weaving work from @Configurable annotated beans when running unittests in eclipse which work just fine when running with mvn test.

            When running in eclipse it seems that the annotated beans does not anyting injected. In the testcase I get my bean from hibernate with a dao.findById. My testcase subclass AbstractTransactionalSpringContextTests in case it matter.


            -Kaj

            Comment


            • #7
              I personally found this plugin not working as expected. It does not weave aspects from other jars unless you also include the required modules in the weaveDependencies section. But the latter causes build to include the aspects into resulting jar files. I offer another plugin, which does weaving smarter: http://anydoby.com/jblog/article.htm?id=68&lng=en

              Comment

              Working...
              X