MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap
Revision 22 as of 2014-05-27 16:24:38
  • Java
  • ApacheMaven

Apache Maven

Maven is a software project management and comprehension tool. Based on the concept of a Project Object Model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

Requires Java.

Slackbuild

  • su
  • cd /tmp
  • wget http://slackbuilds.org/slackbuilds/14.0/development/apache-maven.tar.gz

  • tar xvzf apache-maven.tar.gz
  • cd apache-maven
  • wget http://archive.apache.org/dist/maven/binaries/apache-maven-3.0.4-bin.tar.gz

  • ./apache-maven.SlackBuild

  • installpkg /tmp/apache-maven-3.0.4-noarch-1_SBo.tgz

Package: apache-maven-3.0.4-noarch-1_SBo.tgz

User settings

You can specify your user configuration in ${user.home}/.m2/settings.xml. The default location of your local repository is ${user.home}/.m2/repository/.

Maven sample web app for JBoss AS 7.1.1

Web application creation

  • cd ~
  • mvn archetype:generate -DgroupId=org.allowed.bitarus -DartifactId=WebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

  • cd WebApp

  • mvn clean
  • mvn compile
  • mvn package

Deploy web application on jboss AS 7.1.1

  • /opt/jboss-as-7.1.1.Final/bin/standalone.sh
  • http://localhost:9990

  • admin 12345678
  • Manage deployments
  • Add content
  • Choose file ~/WebApp/target/WebApp.war
  • next, save
  • enable , confirm
  • http://localhost:8080/WebApp/

Add REST and servlet support

  • url http://localhost:8080/WebApp/rest/testSvc/paramx

Add dependencies to pom.xml, jboss AS 7.1.1 JAX-RS

   1 <dependency>
   2     <groupId>org.jboss.resteasy</groupId>
   3     <artifactId>resteasy-jaxrs</artifactId>
   4     <version>2.2.1.GA</version>
   5     <scope>provided</scope>
   6 </dependency>
   7 <dependency>
   8     <groupId>javax</groupId>
   9     <artifactId>javaee-web-api</artifactId>
  10     <version>6.0</version>
  11     <scope>provided</scope>
  12 </dependency>

In JBoss 5.1.0 comment the scope provided for resteasy, to add to WEB-INF folder the required libraries for resteasy.

Create source code for REST service and servlet

File src/main/java/org/allowed/bitarus/TestService.java

URL http://localhost:8080/WebApp/rest/testSvc/asddddd

   1 package org.allowed.bitarus;
   2 
   3 import javax.ws.rs.GET;
   4 import javax.ws.rs.Path;
   5 import javax.ws.rs.PathParam;
   6 import javax.ws.rs.core.Response;
   7 
   8 @Path("/testSvc")
   9 public class TestService {
  10 
  11     @GET
  12     @Path("/{param}")
  13     public Response getMsg(@PathParam("param") String msg) {
  14         String out = String.format("testSvc returns %s", msg);
  15         return Response.status(200).entity(out).build();
  16     }
  17 
  18 }

File src/main/java/org/allowed/bitarus/Servletx.java

http://localhost:8080/WebApp/servletxxx

   1 package org.allowed.bitarus;
   2 
   3 import javax.servlet.*;
   4 import javax.servlet.http.*;
   5 import java.io.*;
   6 
   7 public class Servletx extends HttpServlet{
   8     public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
   9     {
  10         res.setContentType("text/html");
  11         PrintWriter out = res.getWriter();
  12         out.println("<html><head><title>Hello World!</title></head>");
  13         out.println("<body><h1>Hello World!</h1></body></html>");
  14     }
  15 }

Configure web app

File src/main/webapp/WEB-INF/web.xml

   1 <web-app>
   2   <display-name>Archetype Created Web Application</display-name>
   3   <context-param>
   4     <param-name>resteasy.scan</param-name>
   5     <param-value>true</param-value>
   6   </context-param>
   7  
   8   <context-param>
   9     <param-name>resteasy.servlet.mapping.prefix</param-name>
  10     <param-value>/rest</param-value>
  11   </context-param>
  12 
  13   <servlet>
  14     <servlet-name>resteasy-servlet</servlet-name>
  15     <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
  16   </servlet>
  17   <servlet-mapping>
  18     <servlet-name>resteasy-servlet</servlet-name>
  19     <url-pattern>/rest/*</url-pattern>
  20   </servlet-mapping>
  21 
  22   <servlet>
  23     <servlet-name>Servletx</servlet-name>
  24     <servlet-class>org.allowed.bitarus.Servletx</servlet-class>
  25   </servlet>
  26   <servlet-mapping>
  27     <servlet-name>Servletx</servlet-name>
  28     <url-pattern>/servletxxx</url-pattern> 
  29   </servlet-mapping>
  30 </web-app>

Dependencies scopes on pom.xml

compile

compile is the default scope; all dependencies are compile-scoped if a scope is not supplied. compile dependencies are available in all classpaths, and they are packaged.

provided

provided dependencies are used when you expect the JDK or a container to provide them.

Eclipse plugin

Install on Eclipse 3.8.2

  • Choose Help menu
  • Install new software
  • Name: M2Eclipse

  • Location: http://download.eclipse.org/technology/m2e/releases

  • Maven Integration For Eclipse
  • Select all
  • next
  • next
  • I accept
  • Finish

Import existing project

  • Java perspective
  • Import
  • Maven
  • Existing Maven project
  • next
  • root folder
  • next
  • finish

Maven sample enterprise app

  • cd /tmp
  • mvn archetype:generate -DgroupId=org.allowed.bitarus -DartifactId=EnterpriseApp -DarchetypeArtifactId=org.jboss.spec.archetypes.jboss-javaee6-ear-webapp -DinteractiveMode=false

  • cd EnterpriseApp/
  • Edit pom.xml and comment site module
  • mvn clean
  • mvn compile
  • mvn package # /tmp/EnterpriseApp/EnterpriseApp-ear/target/EnterpriseApp.ear
  • Deploy the EnterpriseApp.ear with http://localhost:9990

  • Open http://localhost:8080/EnterpriseApp/index.jsf

Maven sample ejb JEE5

  • cd /tmp
  • mvn archetype:generate -DgroupId=org.allowed.bitarus -DartifactId=EjbJEE5 -DarchetypeArtifactId=org.codehaus.mojo.archetypes.ejb-jee5 -DinteractiveMode=false

  • cd EnterpriseApp/
  • Edit pom.xml and comment site module
  • mvn clean
  • mvn compile
  • mvn package # /tmp/EnterpriseApp/EnterpriseApp-ear/target/EnterpriseApp.ear

POM EJB 3.0 + JAX-WS + JBoss 5.1.0

For JBoss 5.0. Source code Java below folder ejbModule. Create source folder in Eclipse named ejbModule.

pom.xml

   1 <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">
   2   <modelVersion>4.0.0</modelVersion>
   3   <groupId>org.allowed.bitarus</groupId>
   4   <artifactId>test2</artifactId>
   5   <version>0.0.1</version>
   6   <packaging>ejb</packaging>
   7   
   8   <build>
   9     <sourceDirectory>ejbModule</sourceDirectory>
  10     <resources>
  11       <resource>
  12         <directory>ejbModule</directory>
  13         <excludes>
  14           <exclude>**/*.java</exclude>
  15         </excludes>
  16       </resource>
  17     </resources>
  18     <plugins>
  19       <plugin>
  20         <artifactId>maven-ejb-plugin</artifactId>
  21         <version>2.3</version>
  22         <configuration>
  23           <ejbVersion>3.0</ejbVersion>
  24         </configuration>
  25       </plugin>
  26     </plugins>
  27   </build>
  28   
  29   <dependencies>
  30     <dependency>
  31         <!-- EJB 3.0 -->
  32         <groupId>javax.ejb</groupId>
  33         <artifactId>ejb-api</artifactId>
  34         <version>3.0</version>
  35     </dependency>
  36     <dependency>
  37         <!--jax ws web servies -->
  38         <groupId>sun-jaxws</groupId>
  39         <artifactId>jsr181-api</artifactId>
  40         <version>1.0</version>
  41     </dependency>
  42 
  43 <dependency>
  44         <groupId>javaee</groupId>
  45         <artifactId>javaee-api</artifactId>
  46         <version>5</version>
  47 </dependency>
  48 
  49   </dependencies>
  50 </project>

Source code files in ejbModule/org/allowed/bitarus

   1 //WSTest.java
   2 package org.allowed.bitarus;
   3 
   4 import java.sql.Connection;
   5 import java.sql.PreparedStatement;
   6 import java.sql.ResultSet;
   7 import java.sql.SQLException;
   8 import java.util.ArrayList;
   9 
  10 import javax.ejb.Stateless;
  11 import javax.jws.WebMethod;
  12 import javax.jws.WebService;
  13 import javax.naming.Context;
  14 import javax.naming.InitialContext;
  15 
  16 /**
  17  * Session Bean implementation class WSTest
  18  */
  19 @Stateless
  20 @WebService
  21 // http://localhost:8080/test2-0.0.1/WSTest?wsdl
  22 public class WSTest implements WSTestRemote, WSTestLocal {
  23         private static final String QUERY1 = "SELECT distinct table_name FROM user_tables";
  24         private static final String JNDI_DATASOURCE = "java:OracleDS";
  25 
  26         public WSTest() {
  27         }
  28 
  29         @WebMethod
  30         public String helloWorld() {
  31                 return "Hello world";
  32         }
  33 
  34         @WebMethod
  35         public String[] checkDB() {
  36                 ArrayList<String> list = new ArrayList<String>();
  37 
  38                 Connection conn = null;
  39                 PreparedStatement ps = null;
  40                 ResultSet rs = null;
  41 
  42                 try {
  43                         Context ctx = new InitialContext();
  44                         conn = ((javax.sql.DataSource) ctx.lookup(WSTest.JNDI_DATASOURCE)).getConnection();
  45                         ps = conn.prepareStatement(WSTest.QUERY1);
  46                         rs = ps.executeQuery();
  47                         while (rs.next()) {
  48                                 list.add(rs.getString(1));
  49                         }
  50                 } catch (Exception e) {
  51                         list.add(e.getMessage());
  52                 } finally {
  53                         if (rs != null) {
  54                                 try {
  55                                         rs.close();
  56                                 } catch (SQLException e) {
  57                                         e.printStackTrace();
  58                                 }
  59                         }
  60 
  61                         if (ps != null) {
  62                                 try {
  63                                         ps.close();
  64                                 } catch (SQLException e) {
  65                                         e.printStackTrace();
  66                                 }
  67                         }
  68                         if (conn != null) {
  69                                 try {
  70                                         conn.close();
  71                                 } catch (SQLException e) {
  72                                         e.printStackTrace();
  73                                 }
  74                         }
  75                 }
  76                 
  77                 String[] r = new String[list.size()];
  78                 list.toArray(r);
  79                 return r;
  80         }
  81 }

   1 //WSTestLocal.java
   2 package org.allowed.bitarus;
   3 import javax.ejb.Local;
   4 
   5 @Local
   6 public interface WSTestLocal {
   7 }

   1 //WSTestRemote.java
   2 package org.allowed.bitarus;
   3 import javax.ejb.Remote;
   4 
   5 @Remote
   6 public interface WSTestRemote {
   7 }

Build EJB JAR with maven

  • mvn clean compile package

In http://localhost:8080/admin-console/ at link EJB 3.x Application (EJB JAR), add resource test2-0.0.1.jar.

In JBoss 5.1.0 copy the following libraries from the JBOSS_HOME/client directory to the JBOSS_HOME/lib/endorsed directory, so that the JAX-WS 2.0 apis supported by JBossWS are used:

  • jbossws-native-saaj.jar
  • jbossws-native-jaxrpc.jar
  • jbossws-native-jaxws.jar
  • jbossws-native-jaxws-ext.jar
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01