Category Archives: Maven EAR Plugin

Maven Archetype Plugin: Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.3:create-from-project

PROBLEM

When running mvn archetype:create-from-project with Maven Ear Plugin defined under <pluginManagement>….

<project ...>
    ...
    <build>
        <pluginManagement>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-archetype-plugin</artifactId>
                    <version>2.3</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>2.10.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

… the following exception occurs…

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.3:create-from-project (default-cli) on project myproject-webapp: null: MojoFailureException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.3:create-from-project (default-cli) on project myproject-webapp: null
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoFailureException
        at org.apache.maven.archetype.mojos.CreateArchetypeFromProjectMojo.execute(CreateArchetypeFromProjectMojo.java:258)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        ... 19 more

SOLUTION

It appears this problem happens when using Maven Archetype Plugin 2.3, but works fine when using 2.2.

To fix this, define an empty <modules> under <configuration> to prevent NullPointerException.

<project ...>
    ...
    <build>
        <pluginManagement>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-archetype-plugin</artifactId>
                    <version>2.3</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>2.10.1</version>
                    <configuration>
                        <modules/>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
Advertisements

java.lang.NoSuchMethodError: javax/persistence/OneToMany.orphanRemoval()Z

PROBLEM

You configure Hibernate using annotations and set orphanRemoval property in @OneToMany.

@Entity
@Table(name = "person")
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "personId")
    private Long id;

    @OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Car> cars = new HashSet<Car>();

	...
}

When you run the application, the application server throws the following exception:-

Caused by: java.lang.NoSuchMethodError: javax/persistence/OneToMany.orphanRemoval()Z
        at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1868)
        at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:767)

In my case, I’m getting this exception when I run on Websphere Application Server (WAS) 7.5.

SOLUTION

The orphanRemoval property in @OneToMany requires JPA 2.x to work. If you already have JPA 2.x in your classpath and you are still getting the error, then JPA 1.x must already been loaded either by your application or by the application server, causing your JPA 2.x to be ignored.

To fix this:-

  1. Add JPA 2.x in your classpath. For example, hibernate-jpa-2.0-api-1.0.1.Final.jar.
  2. Remove any trace of JPA 1.x from your classpath by scanning your dependency tree.
  3. If your application server uses JPA 1.x, you will need to remove or replace that jar with JPA 2.x.
  4. If you are not allowed to replace JPA 1.x jar on the application server (for example, you are not the admin of the server), you will need to invert the application class loader to PARENT_LAST to ensure the JPA 2.x from your application gets loaded first and JPA 1.x from the application server will get ignored.

Inverting class loader for application without replacing JPA 1.x from application server

Assuming you have JPA 2.x in your application classpath and your project structure looks like this:-

myproject
|- myproject-ear
|  |- pom.xml
|- myproject-war
|  |- src
|  |  |- main
|  |     |- java
|  |     |- resources
|  |     |- webapp
|  |- pom.xml
|- pom.xml     

… if you are deploying your application to Websphere Application Server (WAS), create deployment.xml under src/main/application in the EAR module and instruct WAS to load parent last:-

<?xml version="1.0" encoding="UTF-8"?>
<appdeployment:Deployment xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
                          xmlns:appdeployment="http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi"
                          xmi:id="Deployment_1">
 
    <deployedObject xmi:type="appdeployment:ApplicationDeployment" xmi:id="ApplicationDeployment_1"
                    startingWeight="1" warClassLoaderPolicy="SINGLE">
        <classloader xmi:id="Classloader_1" mode="PARENT_LAST"/>
    </deployedObject>
</appdeployment:Deployment>

In the EAR pom.xml, extend the parent POM and configure the Maven EAR Plugin:-

<?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>

	<!-- Extend the parent POM -->
	<parent>
        <groupId>myproject</groupId>
        <artifactId>myproject</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>myproject-ear</artifactId>
    <packaging>ear</packaging>
    <name>${project.artifactId}</name>

    <build>
        <plugins>
		   <!-- Configure Maven EAR Plugin -->
            <plugin>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <displayName>${project.parent.artifactId}</displayName>
                    <modules>
                        <webModule>
                            <groupId>myproject</groupId>
                            <artifactId>myproject-war</artifactId>
                            <bundleFileName>${project.parent.artifactId}.war</bundleFileName>
                            <contextRoot>${context-root}</contextRoot>
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
		<!-- 
		Expose the WAR module to allow the WAR file to be packaged 
		in the EAR file by Maven EAR Plugin 
		-->
        <dependency>
            <groupId>myproject</groupId>
            <artifactId>myproject-war</artifactId>
            <version>1.0</version>
            <type>war</type>
        </dependency>
    </dependencies>
</project>

Your current project structure should look like this now:-

myproject
|- myproject-ear
|  |- src
|  |  |- main
|  |     |- application
|  |        |- deployment.xml
|  |- pom.xml
|- myproject-war
|  |- src
|  |  |- main
|  |     |- java
|  |     |- resources
|  |     |- webapp
|  |- pom.xml
|- pom.xml

Configure the parent POM to be an aggregator too:-

<?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>myproject</groupId>
    <artifactId>myproject</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

	<!-- Make this parent POM to be an aggregator for both EAR and WAR modules -->
    <modules>
        <module>myproject-ear</module>
        <module>myproject-war</module>
    </modules>
</project>

When you run mvn clean package on the parent POM, the EAR file will contain the WAR file and the deployment file that inverts the class loader for this application.