Archive for category Maven

Cannot execute mojo: resources. It requires a project with an existing po m.xml, but the build is not using one.


Problem: Cannot execute mojo: resources. It requires a project with an existing po
m.xml, but the build is not using one.

This error simply means there is not pom.xml in the working directory. From the path,where the mvn jetty:run is being run, pom.xml does not exist.

Navigate to the right directory and run the command being tried again should resolve the issue.

Leave a comment

The plugin ‘org.apache.maven.plugins:maven-jetty-plugin’ does not exist o r no valid version could be found


Problem: The plugin ‘org.apache.maven.plugins:maven-jetty-plugin’ does not exist o
r no valid version could be found

 

Performing mvn jetty:run is throwing this error and infact maven-jetty-plugin does not exist in pom.xml. Instead of adding it every pom.xml that tries to use jetty, instead plug-in is added to settings.xml as below

 

<pluginGroups>
<!– pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
–>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>

Leave a comment

java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet


Problem: Deploying a newly created JSF 2.1 Application on Tomcat 7 and while starting the server, gets the following error, which made me to scratch my head. While working with JSF 1.2, on other IDE’s like MyEclipse, i do not remember coming across this issue.

java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet

Solution:

I give credit to the author at

http://blog.v-s-f.co.uk/2010/09/jsf-2-1-project-using-eclipse-and-maven-2/

and also i added my own findings in this post.

We need to change the below few files so that the jar files are deployed with the application.

So now open the file org.eclipse.wst.common.component which is in the .settings folder. Delete the lines:

<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/test/java” />
<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/test/resources” />

Replace them with:

<dependent-module deploy-path=”/WEB-INF/lib”>
<dependency-type>uses</dependency-type>
</dependent-module>

Open .classpath and replace the line: <classpathentry exported=”true” kind=”con” path=”org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER”/> with:

<classpathentry exported=”true” kind=”con” path=”org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER”>
<attributes>
<attribute name=”org.eclipse.jst.component.dependency” value=”/WEB-INF/lib”/>
</attributes>
</classpathentry>

Apart from doing the above steps, without doing this step, still throwed the exception for me. So, do this as well, right click on project -> properties -> java build path and look for the tab Libraries. Make sure that JavaSE 1.6 is available. If not, remove the old one (default is 1.4 in eclipse)  if any, click Add Library on right and add the JavaSE 1.6.

Navigate to Order and Export and select JavaSE 1.6 along with Maven Dependencies.

After selecting Maven Dependencies, navigate to Project Facets, if you do not happen to see the Project Facets list, close the properties dialog box refresh your project and then revisit the Project Facets.

 

In Eclipse, .project should look like

<?xml version=”1.0″ encoding=”UTF-8″?>
<projectDescription>
<name>projname</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

.classpath should look like

<?xml version=”1.0″ encoding=”UTF-8″?>
<classpath>
<classpathentry kind=”src” output=”target/classes” path=”src/main/java”/>
<classpathentry excluding=”**” kind=”src” output=”target/classes” path=”src/main/resources”/>
<classpathentry kind=”src” output=”target/test-classes” path=”src/test/java”/>
<classpathentry excluding=”**” kind=”src” output=”target/test-classes” path=”src/test/resources”/>
<classpathentry kind=”src” path=”src/main/webapp”/>
<classpathentry exported=”true” kind=”con” path=”org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER”>
<attributes>
<attribute name=”org.eclipse.jst.component.dependency” value=”/WEB-INF/lib”/>
</attributes>
</classpathentry>
<classpathentry exported=”true” kind=”con” path=”org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6″/>
<classpathentry kind=”con” path=”org.eclipse.jst.j2ee.internal.web.container”/>
<classpathentry kind=”con” path=”org.eclipse.jst.j2ee.internal.module.container”/>
<classpathentry kind=”output” path=”target/classes”/>
</classpath>

org.eclipse.wst.common.component as

<?xml version=”1.0″ encoding=”UTF-8″?>
<project-modules id=”moduleCoreId” project-version=”1.5.0″>
<wb-module deploy-name=”projname”>
<wb-resource deploy-path=”/” source-path=”/WebContent”/>
<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/main/java”/>
<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/main/resources”/>
<dependent-module deploy-path=”/WEB-INF/lib”>
<dependency-type>uses</dependency-type>
</dependent-module>
<wb-resource deploy-path=”/WEB-INF/classes” source-path=”/src/main/webapp”/>
<property name=”context-root” value=”projname”/>
<property name=”java-output-path” value=”/projname/target/classes”/>
</wb-module>
</project-modules>

pom.xml for JS2.1 App

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.proj</groupId>
<artifactId>projname</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>clm prowise</name>
<description>    </description>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2</url&gt;
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.0-b03</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.0-b03</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

4 Comments

annotations are not supported in -source 1.3


Problem: I am using maven 3 and while building a war file, i got this error as one of my java class files has annotations.

annotations are not supported in -source 1.3

To avoid this error, add the below plugin to pom.xml

Solution:

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

Leave a comment

Maven web resources filtering


Problem: How to perform maven filtering for resources inside webapp

Solution: To acheive this, we need to add the below plugin inside pom.xml and include the resources that need to be filtered, in this example, its *.html

<!– Enabling and configuring web resources filtering –>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/*.html</include>
<!– include any other file types as needed –>
</includes>
</resource>
</webResources>
</configuration>
</plugin>

once the resources are filtered, pom specific settings will be available in the html, for example version of the project can be accessed from the html as ${project.version}

Leave a comment

Axis2M


Axis2M is a utility projects that simplify Axis2 web service development along with Maven. axis2M provide set of Maven artifacts and promote usage of j2ee web application project layout for axis2 development:

Axis2mhas following features.

  • quickstart archetype
    • provide easy start for Hello world axis2 web service only using few number of Maven commands . Also this genarate standred j2ee project layout for axis 2 peoject, and provide WAR pacakgeing option for both services and modules.
  • axis2m:run goal to to run Axis2 simple Http server
    • This Maven plug-in facilitates to run Axis2’s simple Http server with Maven, those who familiar with axis2 standard distribution can use same server.
  • axis2m:WSDL2Java goal to simplfy code genaration under Maven project
    • This is a customized version of Axis2 WSDL2Code maven plug-in; this will generate java codes with minimum number of configuration options and garneted code are compatible with Maven project structure.
  • Pre-configuration of axis2 ARR, MAR plug-ins for quickstart archetype.
    • Once the project is created using axis2m quickstart archetype axis2 AAR and axis2 MAR maven plug-ins are configured with project layout so that users not required configure POM file again.

This tutorial briefs the steps involved for Hello world axis2 web service only using few number of Maven command. Before running the below steps, it is assumed that maven is available on your path, if not set up maven on your environmental path.

1. Run maven archetype generate

mvn archetype:generate -DarchetypeCatalog=http://axis2m.sourceforge.net/repo

2. Choose options

3. From eclipse, select IMPORT EXISTINGECTS -> MAVEN -> Exiting Maven Projects

4.Click next, select quickstart:1.0:war

5.

6.

7.

thats it…it was a breeze to get started and see Axis2 in action in a short time..Run the client.java to hit the service.

Leave a comment

Configuring Maven + Hibernate tools


Problem:  Database schema is available and need to generate hibernate configuration file, mapping files by reverse engineering. Use the generated hibernate mapping files to create POJO annotations and thereby use the annotated files for database persistence.

Solution:

This can be done using hibernate3-maven-plugin and using the goals as hbm2cfgxml,hbm2hbmxml and hbm2java  as shown below. As of now, i did this in two steps even though i am showing the entire pom here at one shot. (i.e comment execution for step2 while doing step1 and vice versa)

Step1: Generate the hibernate.cfg.xml and the mapping files.
Step2: Manally update the hibernate.cfg.xml with the <mapping resources=”path to above generated mapping.hbm.xml” /> files and then Generate the annotated classes

<build>
<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>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>hbm2cfgxml</id>
<phase>generate-resources</phase>
<goals>
<goal>hbm2cfgxml</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2cfgxml</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>target/classes</outputDirectory>
</component>
</components>
</configuration>
</execution>

<execution>
<id>hbm2hbmxml</id>
<phase>generate-resources</phase>
<goals>
<goal>hbm2hbmxml</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2hbmxml</name>
<outputDirectory>target/classes</outputDirectory>
</component>
</components>
<componentProperties>
<packagename>provide.your.package.name</packagename>
</componentProperties>
</configuration>
</execution>

<execution>
<id>hbm2java</id>
<phase>generate-sources</phase>
<goals>
<goal>hbm2java</goal>
</goals>
<inherited>false</inherited>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>annotationconfiguration</implementation>
</component>
</components>
<componentProperties>
<implementation>annotationconfiguration</implementation>
<jdk5>true</jdk5>
<ejb3>true</ejb3>
<format>true</format>
<export>false</false>
<configurationfile>target/classes/hibernate.cfg.xml</configurationfile>
</componentProperties>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
……………………
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Step 1 is fairly straightforward. To perform step 1, run the below command

mvn generate-resources

which  will generate configuration file (hbm2cfxml goal) in target/classes and hibernate mappings (hbm2hbmxml goal) in target/classes as well. Now you need to COPY them to your src/main/resources (in eclipse for example) and update the hibernate.cfg.xml with
the <mapping resources=”path to all the above generated mapping.hbm.xml” /> files

I faced issues while generating annotated classes in Step 2, key thing to generate annotated classes, with ejb3 annotations for entity classes (like @Entity, @Id etc) is to set the flag <ejb3>true<ejb3> and also provide <jdk5>true</jdk5> to turn on generics. Now comment step1 executions in pom.xml and  run the below command

mvn generate-sources

which will generate annotated classes by default at target\hibernate3\generated-sources. You can copy them over to your source and use them with the DAO’s for java persistence

,

Leave a comment