Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate field name "ajc$tjp_0" #54

Open
imochurad opened this issue Oct 11, 2019 · 2 comments
Open

Duplicate field name "ajc$tjp_0" #54

imochurad opened this issue Oct 11, 2019 · 2 comments

Comments

@imochurad
Copy link

I am using aspectj-maven-plugin to weave my sources and tests.
I have a spring-boot app, in which I want to use compile-time weaving of my code to manage JDBC transactions. Here is my portion of the maven file:

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.8</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.3.1.Final</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>-Amapstruct.defaultComponentModel="spring"</compilerArg>
                        <compilerArg>-Amapstruct.unmappedTargetPolicy=WARN</compilerArg>
                        <compilerArg>-Amapstruct.suppressGeneratorTimestamp=true</compilerArg>
                        <compilerArg>-Amapstruct.suppressGeneratorVersionInfoComment=true</compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <proc>none</proc>
                    <Xlint>ignore</Xlint>
                    <forceAjcCompile>true</forceAjcCompile>
                    <complianceLevel>${java.version}</complianceLevel>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <showWeaveInfo>true</showWeaveInfo>
                    <sources/>
                    <testSources/>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                    </weaveDirectories>
                    <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>aspectjtools</artifactId>
                        <version>1.8.13</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>1.8.13</version>
                    </dependency>
                </dependencies>
            </plugin>

After the compilation phase, the integration test starts running, but it fails immediately with:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [my_package.microservice.MyManager] for bean with name 'myManager' defined in file [/my_project/service/target/test-classes/my_package/MyManager.class]: problem with class file or dependent class; nested exception is java.lang.ClassFormatError: Duplicate field name "ajc$tjp_0" with signature "Lorg.aspectj.lang.JoinPoint$StaticPart;" in class file my_package/MyManager
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1383)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:668)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:635)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1489)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:420)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:390)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:511)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:503)
	at org.springframework.boot.context.TypeExcludeFilter.match(TypeExcludeFilter.java:65)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:492)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:431)
	... 51 common frames omitted

How do I solve this issue?

@PhilGBr
Copy link

PhilGBr commented Dec 29, 2019

Hi Imochurad,

the same thing happened to me, with a very similar AspectJ weaving scheme (i.e weaving already compiled classes produced in the Maven build during the compilation phase).

In my case, the reason was that the classes to be woven (located in the weaveDirectory directory ) were woven twice:

  • the first time, when executing the goal <compile>
  • the second time, when executing the goal <test-compile>

With this weaving scheme (as opposed to specifying classes to be woven by their source files with <sources>/<testSources> ... <include>/<exclude> directives), I found the XnotReweavable parameter convenient.

Adding:

<XnotReweavable>true</XnotReweavable>

to the <configuration> element did the trick: even if the same weaveDirectory is used both for <compile> and <test-compile> goals, classes are not rewoven, and so the Duplicate field name "ajc$tjp_0" exception raised when executing the unit tests disappears

Another (cleaner, but not tested yet) solution would be to have two distinct <weaveDirectory> configurations for test and compile goals.

For the <goal>test-compile</goal>, one could define the tests compilation target directory ${project.build.directory}/test-classes as the <weaveDirectory>

Ending up with something like:

image

I hope this helps someone

@PhilGBr
Copy link

PhilGBr commented Dec 29, 2019

I tested the second solution, and it worked fine for me.

Note: shared settings can be defined in a the top level <configuration> element (no need to duplicate them at the <execution> level)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants