Errors when executing maven install with glassfish-embedded-all and arquillian-glassfish-embedded-3.1 artifacts - maven

I'm trying to execute maven install on a pom and the result shown is:
Grave: SEC5054: Certificate has expired
This result appears just after test execution begins.
I have been googling over this problem but I've only found solutions that are related to a real glassfish application server. They recommend things like deleting the offending certificates from the folder where they are located and so on (the pages I've seen are mostly like this) or 'unjar' the glassfish-embedded to remove the certification and then jar it again.
Notice that I'm executing a maven install, not an actual deployment on an application server. That's why I cannot take the advice given at many blogs
The pom includes the following dependencies:
<dependencies>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.0.0.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR3</version>
<scope>test</scope>
</dependency>

This is a known issue at least since Glassfish 3.0.1 and still reported as an Open Issue in Glassfish 3.1.2. Oracle provides some workarounds - that all doesn't apply to your situation. But they also say:
If the instance is not configured this way, ignore the warning. The functionality of the instance is unaffected.
So even if this is a lame answer to a SO question: Don't bother for your test-cases. (Personally I spend far to much time trying to solve the issue.) Either a new version of Glassfish will resolve this issue for us or it will not. Let's stop bothering.
Update:
If you have problems that this triggers a failed build, the following pom works for me without a failed build:
<!--snip-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.0.3.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.7.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

You can try this:
Create a custom domain.xml
Change <jvm-options>-Djavax.net.ssl.trustStore=${com.sun.aas.instanceRoot}/config/cacerts.jks</jvm-options> to <jvm-options>-Djavax.net.ssl.trustStore=insertpathtocacert/cacerts.jks</jvm-options>
Configure arquillian.xml setting configurationXml to be the path to domain.xml: https://docs.jboss.org/author/display/ARQ/GlassFish+3.1+-+Embedded
or this: Arquillian Embedded Glassfish Certificate Expired

I assume that the test phase starts the embedded glassfish server which is responsible for the error. Install phase is after Test phase. If you want to execute any test cases, you have to unjar and remove the conflicting certificates (in .m2 folder). Else you can bypass the test phase by using a flag -DskipTests=true.

Related

Spring aspects woven by AspectJ compiler working in Maven, but not in IntelliJ IDEA

I'm using Spring boot 2.5.5 with AspectJ 1.9.7 (CTW). I've spotted that sometimes transactions don't roll back and to fix that I need only recompile code and run it again. For example:
I have method addB() persisting entity B, method addC() throwing exception and method A() combining them. When I call A(), exception is thrown, but entity B stays in database (as expected). When I annotate method A() with #Transactional result is the same. But if I build everything again (without any changes) then transaction is being rollbacked and there is no new record in database.
Here is my full POM:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.abcc</groupId>
<artifactId>abcc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>abcc</name>
<description>abcc</description>
<properties>
<java.version>11</java.version>
<log4j2.version>2.15.0</log4j2.version>
<aspectj.version>1.9.7</aspectj.version>
<aspectj-maven-plugin.version>1.14.0</aspectj-maven-plugin.version>
</properties>
<dependencies>
<!--SPRING-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--CACHE-->
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.1</version>
</dependency>
<!--DATABASE-->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.18.1</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.19.Final</version>
</dependency>
<dependency>
<groupId>org.passay</groupId>
<artifactId>passay</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.sanctionco.jmail</groupId>
<artifactId>jmail</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.13</version>
</dependency>
<!--AOP-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<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>
</dependencies>
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<activatedProperties>test</activatedProperties>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.191</version>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj-maven-plugin.version}</version>
<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>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<Xlint>ignore</Xlint>
<verbose>true</verbose>
<source>${java.version}</source>
<target>${java.version}</target>
<complianceLevel>${java.version}</complianceLevel>
<encoding>utf-8</encoding>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
<forceAjcCompile>true</forceAjcCompile>
<sources/>
<weaveDirectories>
<weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
</weaveDirectories>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
It's very strange situation, which I can't find information on. Could you help with finding reason to it?
I cannot reproduce the problem because IDEA does not find the Lombok setters. Even when delegating build actions before run to Maven, I get NoSuchMethodError: '...TestEntity.setCode(java.lang.String)'. Next, I am going to try without Lombok. Please note that Lombok and AspectJ do not play nice with each other, see my answer here. Alternatively, you could also make sure that Maven does either of these:
First build with Javac + Lombok, then apply AspectJ binary weaving in a second step, all in one module.
Similar to above, but do the first build step in module A and the second one in a separate module B. Then you have an unwoven and a woven artifact, which you can both use according to your preferences. For example, you could also use the unwoven one and apply transaction aspects via load-time weaving (LTW) while starting the application. See my other answer here for both approaches #1 and #2.
Delombok the source code build the generated sources with the AspectJ compiler in a second build step.
I generated constructors, getters and setters in the IDE instead of using Lombok. Now the project compiles in both IDE and Maven. It behaves exactly as it should. With #Transactional, 0 entities are created, without it 2.
I am not sure if Lombok vs. AspectJ really is the problem due to non-compileability when using Lombok annotations, but it should be easy enough to try without Lombok for you. If it works in your context, too, we found the culprit and can think about implementing one of the 3 approaches mentioned above. Then you can tell me if you have any difficulty in doing so.
Update: I created the two-module version - Javac + Lombok, then Aspect weaving - for you in my fork and also issued pull request #1. I also improved testability a bit. See if that works for you.
Caveat: You cannot simply run DemoApplication from the application-lombok module, because that module is still unwoven and will not show transactional behaviour. But you can simply change the classpath for the run config to the application-aspectj module:
Update: As we found out in the comment section of the other answer, in addition to the problematic Lombok vs. AspectJ compiler configuration, the OP also simply had a problem with his IDE: Using IntelliJ IDEA Community Edition, he was first unaware of, then unable to install the AspectJ plugin, which means that IDEA does not know antyhing about the AspectJ compiler and simply overwrites anything which might have been compiled by AspectJ Maven before with plain Java classes. Therefore, transactional aspects do not work either, unless
either pre-run compilation is disabled and mvn compile started as an additional pre-build step for the corresponding run configuration,
or all build actions for the project are being delegated to Maven via configuration,
the OP buys a licence of IDEA Ultimate and installs the AspectJ plugin.
First of all: your multi-module approach works perfectly in my environment. But then I checked initial MCVE and when I completely removed Lombok, strange behaviour did not disappear. When reading your answers (this one) I have checked "Delegate IDE build/run actions to maven" in IntelliJ settings (Build Tools -> Maven -> Runner) and it started working as it should. In next step I switched this option off and checked "Do not build before run" in run configuration. I do not understand it completely (especially why did it work in old way after second try), but your comment helped me to reach that.
I will research IntelliJ behaviour (in two scenarios console output is almost identical), but if you have idea why does it work like that I would be glad to hear it. You helped me a lot, thank you!
Summary solution:
I have enabled "Do not build before run" in IntelliJ run configuration for my application. Now changes works after first build.

Maven Dependency Management is importing wrong version

I am cloning project to import a 7.13 version dependency as depicted here. As you can see, the camunda-webapp: 7.11.0 (managed from 7.13.0) is creeping in. This is causing some ClassNotFoundExceptions since I need camunda-webapp: 7.13.0 which IS happening correctly in a diff project I dont own. I did a lot of digging and exclusions but I'm not able to figure out where is this whole 7.11.0 (managed from 7.13.0) is even coming from.
How can i force this project to use 7.13.0 throughout?
However, In another project importing 7.13 imports the transitive 7.13 correctly.
EDIT - Updating the POM here to triage the fix:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.camunda.bpm.getstarted</groupId>
<artifactId>camunda-ctil-cockpit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<camunda.spring-boot.version>7.13.0</camunda.spring-boot.version>
<spring-boot.version>2.3.9.RELEASE</spring-boot.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<capitalone.camunda.plugin.version>1.5</capitalone.camunda.plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.capitalone.camunda.plugins</groupId>
<artifactId>plugin-dependencies</artifactId>
<version>${capitalone.camunda.plugin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
<version>${camunda.spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.camunda.bpm.webapp</groupId>
<artifactId>camunda-webapp-webjar</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
<version>${camunda.spring-boot.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.capitalone.camunda.plugins</groupId>
<artifactId>capitalone-uioverlay</artifactId>
<exclusions>
<exclusion>
<groupId>org.camunda.bpm.webapp</groupId>
<artifactId>camunda-webapp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.capitalone.camunda.plugins</groupId>
<artifactId>spring-plugin-commons</artifactId>
</dependency>
<dependency>
<groupId>com.capitalone.camunda.plugins</groupId>
<artifactId>custom-engine-types</artifactId>
<exclusions>
<exclusion>
<groupId>org.camunda.bpm.webapp</groupId>
<artifactId>camunda-webapp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.capitalone.camunda.plugins</groupId>
<artifactId>common-cockpit-extensions</artifactId>
<exclusions>
<exclusion>
<groupId>org.camunda.bpm.webapp</groupId>
<artifactId>camunda-webapp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.camunda.bpm.identity</groupId>
<artifactId>camunda-identity-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>camunda-ctil-cockpit</finalName>
</build>
</project>
However this results still in. As you can see 7.11 is still there. If I exclude 7.11 completely, the camunda-webapp.jar completely disappears. Was hoping 7.13 would get popped in. Also camunda-webapp-webjar is completely removed, comparing to the screenshots above :
First change the property value of
<camunda.spring-boot.version>3.2.0</camunda.spring-boot.version>
to 7.13.0
See e.g. https://start.camunda.com/
If the issue persists the the plugin jars define their own versions, not driven by the BOM:
You shared the main project POM including the engine and webapps, but the unwanted dependency is coming in as a dependency of your custom com.capitalone.* jars, as the screenshot of the dependency tree shows. camunda-webapp:7.11.0 is a dependency which is pulled in by
1)
<dependency>
<groupId>com.capitalone.camunda.plugins</groupId>
<artifactId>capitalone-uioverlay</artifactId>
</dependency>
2)
<dependency>
<groupId>com.capitalone.camunda.plugins</groupId>
<artifactId>custom-engine-types</artifactId>
</dependency>
3)
<dependency>
<groupId>com.capitalone.camunda.plugins</groupId>
<artifactId>common-cockpit-extensions</artifactId>
</dependency>
A) If possible update the POMs of those projects and run a mvn clean install on them
B) If you have no control over these projects then you can also add exclusions to the dependency tags listed above (as the jar is already included in the main project). However, this would mean your plugin jars running with another jar version than they have been compiled against. It is not a good practice and may cause issue if compatibility changed.
On a different note:
"Please note that we updated the frontend plugin interface with Camunda Platform Runtime 7.14. Plugins written for Camunda Platform Runtime 7.13 and earlier might no longer work with Camunda Platform Runtime 7.14. Checkout the update guide for more details." https://docs.camunda.org/manual/latest/webapps/cockpit/extend/plugins/

Mockito Powermock Test run fails with ClassNotFoundException: org.mockito.cglib.proxy.MethodInterceptor

I'm migrating a maven java8 project to Java 11. All the source files and test files compiles successfully. There are tests that makes use of mockito/powermock.
When I try to mvn clean install, the build fails because of the failures in tests. All the tests fails with the same cause (java.lang.ClassNotFoundException: org.mockito.cglib.proxy.MethodInterceptor) I'm using surefire plugin version 3.3.0-M3.
Please see relevant sections in pom.xml below. Please help me to resolve these ClassNotFoundException and make the build success.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>
--illegal-access=permit
</argLine>
</configuration>
</plugin>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
UPDATE:
The exception is coming from the APIs of following dependency which requires mockito 1.9.5.
<dependency>
<groupId>com.googlecode.catch-exception</groupId>
<artifactId>catch-exception</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>

Unable to configure maven dependencies correctly

I have a web-app with following dependencies in pom:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.9.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
<scope>test</scope>
</dependency>
With this configuration, I get following error in junit test:
java.lang.ClassFormatError: Absent Code attribute in method that is
not native or abstract in class file javax/persistence/Persistence
I found some info on this error here.
So, I removed 'javaee-web-api' dependency.
After that I get compilation error 'package javax.persistence does not exist', and many more.
So, I changed scope of 'hibernate-entitymanager' to default (compile).
The working pom (dependencies) look like this:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.9.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
</dependency>
Now everything is OK, except generated war contains hibernate jar, which I do not want.
Any suggestion how to fix this, correct maven pom?
If you are working with an in application server, you can replace the hibernate dependency with a (still vendor specific) JPA API with provided scope like this ( example is for JBoss with hibernate):
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<scope>provided</scope>
</dependency>
Finally, I got this working, with same set of dependencies, using profiles.
Here is relevant section of pom:
<profiles>
<profile>
<id>test</id>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>package</id>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.9.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Now, I use following commands to package and test,
mvn -Ptest test
mvn -Ppackage package -DskipTests
-DskipTests is required, as, otherwise, package goal triggers 'test' causing the same error I started with, as it uses same set of dependencies configured for 'package'.
java.lang.ClassFormatError: Absent Code attribute in method that is
not native or abstract in class file javax/persistence/Persistence
I was having the same problem and was able to solve it by adding a couple of dependencies to the Maven pom.xml file. See this answer for more details.

How to use a maven package as both a dependency and a plugin

I am putting together a new project using Maven and JUnit. I am using the t7 plugin to run the application under tomcat after maven has compiled and run tests. I didn't have any problems until I wanted to set up the javaURLContextFactory in a JUnit test. The javaURLContextFactory is in the t7 plugin, but not in any of the dependencies for the project. With the plugin only defined as a plugin, the JUnit test fails, as it can't find the javaURLContextFactory class. If add the plugin as a dependency only, The JUnit test works, bu then it can't find the plugin when I want to run or debug. If I define it in both, I get bizarre errors related to parsing the web.xml.
Here is my current pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.star2star</groupId>
<artifactId>distribute</artifactId>
<packaging>war</packaging>
<version>0.8.1-SNAPSHOT</version>
<name>distribute Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>smtp</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0-rc1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-api</artifactId>
<version>7.0.19</version>
</dependency>
</dependencies>
<build>
<finalName>distribute-v1</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>com.googlecode.t7mp</groupId>
<artifactId>maven-t7-plugin</artifactId>
<version>0.9.10.M8</version>
</plugin>
</plugins>
</build>
</project>
Is there any way to reference the classes in the plugin as a dependency, or reference the dependency as a plugin? Is there some other method of fixing this?
Found a solution after talking to a co-worker.
By including the plugin as a dependency, I know it's bad, and setting the scope to provided, it was available for the JUnit test, but not included in the war when it was deployed using the t7 plugin. Then the unit tests were able to run, with the javaURLContextFactory and it still deployed properly.

Resources