limit jetty scanning in maven plugin - java

I'm having problems getting a webapp to start quickly in using the maven jetty plugin in eclipse. I'm using the jetty:run goal.
After turning on the logging the problem seems to be that jetty scans all the jars in my webapp for web app configuration. Just including a dependency to jersey-media-moxy causes jetty to add 48 seconds to its start up time.
How do I limit this scanning in the jetty-maven-plugin? I've found Jetty startup delay due to scanning but before I make some external jetty configuration and include this is the maven configuration, I want to make sure that there isn't a more straightforward option.
http://eclipse.org/jetty/documentation/current/quickstart-webapp.html looks promising, but I'm unsure how to proceed (as this doesn't mention the maven plugin either).
my pom.xml file is included below:
<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.my-app</groupId>
<artifactId>my-app</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>my-app</name>
<build>
<finalName>my-app</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.9.v20150224</version>
<configuration>
<war>${project.basedir}/target/my-app.war</war>
<stopPort>8088</stopPort>
<stopKey>foo</stopKey>
<stopWait>10</stopWait>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- artifactId>jersey-container-servlet-core</artifactId -->
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<!-- uncomment this to get JSON support -->
<!-- -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<!-- -->
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

This is what I use for my projects to speed up Jetty startup just from the maven pom.xml file (no external configuration is required):
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<webApp>
<!-- no need to scan anything as we're using servlet 2.5 and moreover, we're not using ServletContainerInitializer(s) -->
<!-- for more relevant information regarding scanning of classes refer to https://java.net/jira/browse/SERVLET_SPEC-36 -->
<webInfIncludeJarPattern>^$</webInfIncludeJarPattern>
<containerIncludeJarPattern>^$</containerIncludeJarPattern>
<!--<webInfIncludeJarPattern>.*/spring-[^/]*\.jar$|.*/.*jsp-api-[^/]\.jar$|./.*jsp-[^/]\.jar$|./.*taglibs[^/]*\.jar$</webInfIncludeJarPattern>-->
</webApp>
</configuration>
</plugin>
Use any regex of your choice (if you're using servlet initializers). Note that ^$ excludes everything.

Related

org.json.* is not accessible in JAVA Maven Project as a dependency

I am making a JavaFX project (for the GUI) on Maven and for the moment I want to convert a StringBuffer object to JSON.
I tried to add the dependency org.json from Maven but there is a problem in pom.xml file
Missing Artifact org.json:json:bundle:20220924
I tried adding an older version of org.json dependency, it had the same issue
I tried a different dependency, everything was ok in the pom.xml file but I could not import it into my project (any dependency).
All dependencies return this error: The type org.json.JSONArray is not accessible
Lastly, I tried to download the json-java.jar from github and add it into a libs folder on the base project directory and added it in the pom file as a dependency but it could not be resolved either.
My pom.xml file:
<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.example</groupId>
<artifactId>books</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/json-20220924.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>com.example.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
In the end I downloaded Maven again and extracted it into a folder and changed my environment variables on path. Maven is successfully installed on version 3.8.7
Thank you in advance for any help!
UPDATE
I should mention that I tried it on a new java project (without maven) and it works fine, by importing the .jar file from git-hub repository

Vaadin 14 training "RouterAPI" is probably outdatedt, so mvn jetty:run raises an error. How do I need to change the POM file?

as stated I have trouble running mvn jetty:run with the existing vaadin exercise for routing in vs code. As the tutorial was already created under vaadin 10 the pom file is probably outdated, as the other trainigs worked perfectly fine with the mvn jetty:run command.
The exact error is:
No plugin found for prefix 'jetty' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:\Users\Dukatz\.m2\repository), central (https://repo.maven.apache.org/maven2)]
Sadly I'm pretty new do web application development, so I don't have the slightest idea, how to make it work. Hopefully you can help me.
The Tutorial: Vaadin Router Api Tutorial
the POM file:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vaadin.trainings.routing</groupId>
<artifactId>exercises-routing</artifactId>
<name>Routing Exercises</name>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Dependencies -->
<vaadin.version>13.0.5</vaadin.version>
<servlet.api.version>3.1.0</servlet.api.version>
<!-- Plugins -->
<jetty.version>9.4.15.v20190215</jetty.version>
<maven.war.plugin.version>3.1.0</maven.war.plugin.version>
</properties>
<repositories>
<repository>
<id>vaadin-prereleases</id>
<name>Vaadin Pre-releases</name>
<url>https://maven.vaadin.com/vaadin-prereleases</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<type>pom</type>
<scope>import</scope>
<version>${vaadin.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Added to provide logging output as Flow uses -->
<!-- the unbound SLF4J no-operation (NOP) logger implementation -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-core</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven.war.plugin.version}</version>
<configuration>
<archive>
<manifestEntries>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>
</plugins>
</build>
</project>
There is no Problem with the pom file. Because it's an older lesson, there was no pom file created for the whole project, but only in the specific paths (like \exercises or \solutions). So running mvn jetty:run couldn't work. There was no pom in the directory. I started it in VS-Code with the Maven-Explorer jetty plugin but
mvn jetty:run -f "c:\Users\Dukatz\Desktop\VadinTutorials\Vaadin14-RouterAPI\Exercises\pom.xml"
would do the job from the command line.
Thanks me! I won't delete the question because, one day there's maybe someone as dumb as me.

Spring boot maven build not including artifact version in final jar

On building my spring boot app using mvn clean package, the final artifact that is getting built, myapp.jar, is not having the version number included in the jar name.
What change should i do to get the artifact verion number to be part of jar.
The relevant part of pom.xml is as below -
<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.mycomp</groupId>
<artifactId>myapp</artifactId>
<version>2.6.0</version>
<name>myapp</name>
<description>
<![CDATA[]]>
</description>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<springboot.version>2.5.6</springboot.version>
</properties>
<dependencies>
<!-- Spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.12</version>
</dependency>
...
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>
<build>
<finalName>gitlab-finder</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
</plugin>
</plugins>
</build>
</project>
Note: I created a new spring boot project using spring starter (through sts), and for this new project the jar has artifact version included in the jar name by default. So wondering if this a default feature why is this not the case for my project (by the way, i am maintainer of this project not the creator but i have to add this feature of including version number to jar name.)
You are overwriting the default behaviour of generating project names. This is because you define the finalName tag.
From the docs:
finalName: This is the name of the bundled project when it is finally built (sans the file extension, for example: my-project-1.0.jar). It defaults to ${artifactId}-${version}.
In your case it is defined as <finalName>gitlab-finder</finalName> which does not contain the version of the project. You either add ${version} to it <finalName>gitlab-finder-${version}</finalName> or remove the finalName tag.

How to configure Surefire to include '*Spec' Spock tests?

How can I make sure that certain tests are executed in a Maven build and not silently ignored?
I was recently forced to switch to Groovy 3. As a result, I updated my Spock version as well. Unfortunately, I missed the fact that Spock 2 requires JUnit 5. I only had JUnit 4 and Spock tests, therefore the Maven Surefire plugin used the JUnit 4 provider to execute tests. All Spock test were ignored. I just noticed this by accident.
I'm looking for a way to check if certain (or any) tests ending in *Spec are among the tests executed. I looked at the Maven Surefire and the Maven Enforcer plugin but could not find anything which suits my needs. If I'm skipping test execution completely this check should not fail.
Edit: This is a shortened version of the Spock example project as kriegaex suggested posting some pom.xml files. The problem applies to all Java projects, though. To me, a solution within the build process of the project itself would be superior to configuring CI/CD jobs as this can can easily be forgotten when moving/migrating the project to another CI pipeline.
<?xml version="1.0"?>
<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>org.spockframework</groupId>
<artifactId>spock-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spock Framework - Example Project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<groovy.version>3.0.7</groovy.version>
</properties>
<build>
<plugins>
<!-- Mandatory plugins for using Spock -->
<plugin>
<!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
visit https://github.com/groovy/GMavenPlus/wiki -->
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.12.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Optional plugins for using Spock -->
<!-- Only required if names of spec classes don't match default Surefire patterns (`*Test` etc.) -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<useModulePath>false</useModulePath> <!-- https://issues.apache.org/jira/browse/SUREFIRE-1809 -->
<useFile>false</useFile>
<includes>
<include>**/*Test</include>
<include>**/*Spec</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-bom</artifactId>
<version>2.0-M5-groovy-3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Mandatory dependencies for using Spock -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-junit4</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sorry for the misunderstanding about spock-junit4. You only need that if you want to run JUnit 4 extensions, such as PowerMock runner, under Spock 2. If you want to run JUnit 4 tests beside Spock 2 tests, you need org.junit.vintage:junit-vintage-engine:5.7.1 for Spock 2.0-M5. Adding that dependency, fixes your POM for me.
However, I took the liberty to upgrade to Spock 2.0 final, Groovy 3.0.8 and JUnit 5.7.2, all in harmony with Spock 2.0. If you use a default Maven directory layout and name your Spock specs src/test/groovy/*Test instead of *Spec, you can even do without the Surefire class name filter. You should however specify Java source and target levels. Without them, my JDK complained about the default 1.5.
<?xml version="1.0"?>
<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>org.spockframework</groupId>
<artifactId>spock-example</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Spock Framework - Example Project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<groovy.version>3.0.8</groovy.version>
</properties>
<build>
<plugins>
<!-- Mandatory plugins for using Spock -->
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.12.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<useModulePath>false</useModulePath> <!-- https://issues.apache.org/jira/browse/SUREFIRE-1809 -->
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-bom</artifactId>
<version>2.0-groovy-3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<!-- Required if you want to run JUnit 4 tests alongside Spock 2 -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
P.S.: Actually, first I wanted to close your question as a duplicate of that one. But because since then the version numbers have changed a bit, I thought I would post a new answer here for your convenience.
Update: You can make sure that both testes named *Test and *Spec are executed, by fixing the filter in your question by adding .java or .class extensions. Weirdly enough, .groovy will not work, which is a known Surefire quirk.
<includes>
<include>**/*Test.class</include>
<include>**/*Spec.class</include>
</includes>
or
<includes>
<include>**/*Test.java</include>
<include>**/*Spec.java</include>
</includes>

Java: Enum : NoClassDefFoundError

I am facing issues while using enum in my J2EE application. I am using enum in a switch case inside my stateless service bean.
During runtime I see following exception on switch statement:
Caused by: java.lang.NoClassDefFoundError: com/comp/service/TestServiceImpl$1
This issue has been extensively discussed on one of the thread on SO. But I do not see any solution mentioned to resolve this issue.
In my case I use JBOSS EAP6.1 server. JDK version is 1.7. Code is built using Maven in Eclipse IDE. And application is deployed as EAR archive. How do I add this extra generated class file in classpath inside my EAR archive? Is there any other way to resolve this issue?
Update 29 June 2014:
I tried to build application from command line. Then this extra class file is generated. And I am able to deply and execute application successfully. It seems to be bug with eclipse then. Any idea how to resolve it?
pom.xml from EAR project:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>demo-maven</artifactId>
<groupId>com.comp.demo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>demo-ear</artifactId>
<packaging>ear</packaging>
<name>demo - ear</name>
<url>www.comp.com</url>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<distribution>repo</distribution>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
</license>
</licenses>
<dependencies>
<!-- Depend on the ejb module and war so that we can package them -->
<dependency>
<groupId>com.comp.demo</groupId>
<artifactId>demo-web</artifactId>
<type>war</type>
</dependency>
<dependency>
<groupId>com.comp.demo</groupId>
<artifactId>demo-service</artifactId>
<type>ejb</type>
</dependency>
</dependencies>
<build>
<finalName>${project.parent.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>${version.ear.plugin}</version>
<configuration>
<!-- Tell Maven we are using Java EE 6 -->
<version>6</version>
<!-- Use Java EE ear libraries as needed. Java EE ear libraries
are in easy way to package any libraries needed in the ear, and automatically
have any modules (EJB-JARs and WARs) use them -->
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules></modules>
<fileNameMapping>no-version</fileNameMapping>
</configuration>
</plugin>
<!-- The JBoss AS plugin deploys your ear to a local JBoss EAP container -->
<!-- Due to Maven's lack of intelligence with EARs we need to configure
the jboss-as maven plugin to skip deployment for all modules. We then enable
it specifically in the ear module. -->
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!-- When built in OpenShift the 'openshift' profile will be used when invoking mvn. -->
<!-- Use this profile for any OpenShift specific customization your app will need. -->
<!-- By default that is to put the resulting archive into the 'deployments' folder. -->
<!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
<id>openshift</id>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>${version.ear.plugin}</version>
<configuration>
<outputDirectory>deployments</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
pom.xml from ejb project:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>demo-maven</artifactId>
<groupId>com.comp.demo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>demo-service</artifactId>
<packaging>ejb</packaging>
<name>demo - service</name>
<url>www.comp.com</url>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<distribution>repo</distribution>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
</license>
</licenses>
<dependencies>
<!-- Declare the APIs we depend on and need for compilation. All of them
are provided by JBoss EAP 6 -->
<!-- Import the EJB API, we use provided scope as the API is included in
JBoss EAP 6 -->
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<!-- Import the CDI API, we use provided scope as the API is included in
JBoss EAP 6 -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Import the JPA API, we use provided scope as the API is included in
JBoss EAP 6 -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSR-303 (Bean Validation) Implementation -->
<!-- Provides portable constraints such as #Email -->
<!-- Hibernate Validator is shipped in JBoss EAP 6 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.ejb3</groupId>
<artifactId>jboss-ejb3-ext-api</artifactId>
<version>2.0.0-redhat-2</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<!-- Test scope dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
<scope>test</scope>
</dependency>
<!-- Optional, but highly recommended -->
<!-- Arquillian allows you to test enterprise code such as EJBs and Transactional(JTA)
JPA from JUnit/TestNG -->
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>${version.ejb.plugin}</version>
<configuration>
<!-- Tell Maven we are using EJB 3.1 -->
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!-- The default profile skips all tests, though you can tune it to run
just unit tests based on a custom pattern -->
<!-- Seperate profiles are provided for running all tests, including Arquillian
tests that execute in the specified container -->
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire.plugin}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<!-- An optional Arquillian testing profile that executes tests in your
JBoss EAP instance -->
<!-- This profile will start a new JBoss EAP instance, and execute the
test, shutting it down when done -->
<!-- Run with: mvn clean test -Parq-jbossas-managed -->
<id>arq-jbossas-managed</id>
<dependencies>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-arquillian-container-managed</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile>
<!-- An optional Arquillian testing profile that executes tests in a remote
JBoss EAP instance -->
<!-- Run with: mvn clean test -Parq-jbossas-remote -->
<id>arq-jbossas-remote</id>
<dependencies>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-arquillian-container-remote</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
I ran into a similar issue, and the easy workaround was to define the Enum as public instead of private.
(Didn't have time to verify, but my hunch is that this causes the class to not be created as Name$1.class but rather something like Name$Enumname.class, which seemed to be the problem)
In my case, to avoid that NoClassDefFoundError in the code line of the switch statement, the only solution i saw was to replace switch statement by "else if" statements:
if (enumValue == EnumClass.Enum1) {
....
} else if (enumValue == EnumClass.Enum2) {
....
} else if (enumValue == EnumClass.Enum3) {
....
} else {
....
}
Exception caused by:
Caused by: java.lang.ClassNotFoundException: com.test.services.impl.MyServiceImpl$1
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1358)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
Test with Java 8, Tomcat 8.5, Spring boot.
I have a project configuration quite similar to yours: eclipse, Maven, JDK 1.6, JBoss EAP6.2, and I have the same problem with a java.lang.NoClassDefFoundError when using an enum in a switch case.
I have found a workaround for it: generate an ear file (it is a war in my case) and install it manually from the JBoss administration console. I noticed the war includes the $1 needed classes and then you won't get the exception.
This is just a workaround, but it works for me. A good point of this workaround is that subsequent deployments done through eclipse work!
I will be listening to the answers to your question because I want a real solution.

Categories