Size: 604
Comment:
|
Size: 19056
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
## page was renamed from ApacheMaven | |
Line 18: | Line 19: |
Package: [[attachment: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 Sample WebApp file with source code to use MySQL data source:[[attachment:WebApp.zip]] Sample WebAppJersey2, with dependencies for Jersey2 and JSON, to be deployed on Tomcat 7:[[attachment:WebAppJersey2.zip]] === 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 {{{#!highlight xml <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.1.GA</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </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 {{{#!highlight java package org.allowed.bitarus; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/testSvc") public class TestService { @GET @Path("/{param}") public Response getMsg(@PathParam("param") String msg) { String out = String.format("testSvc returns %s", msg); return Response.status(200).entity(out).build(); } } }}} File src/main/java/org/allowed/bitarus/Servletx.java http://localhost:8080/WebApp/servletxxx {{{#!highlight java // File src/main/java/org/allowed/bitarus/Servletx.java // References EJB in JBoss5. Test to see EJB invocation from Tomcat package org.allowed.bitarus; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.security.Security; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import org.allowed.bitarus.IWSTest; public class Servletx extends HttpServlet{ public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); String text=""; try{ Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099"); InitialContext context = new InitialContext(env); IWSTest test = (IWSTest) context.lookup("WSTest/remote"); text=test.helloWorld(); } catch(Exception ex){ } out.println("<html><head><title>Hello World!</title></head>"); out.println(String.format("<body><h1>Hello World! %s </h1></body></html>",text)); } } }}} Dependency for JAR that has IWSTest interface that WSTest EJB implements: {{{#!highlight xml <dependency> <groupId>org.allowed.bitarus</groupId> <artifactId>test2</artifactId> <version>0.0.1</version> </dependency> }}} === Configure web app === File src/main/webapp/WEB-INF/web.xml {{{#!highlight xml <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/rest</param-value> </context-param> <servlet> <servlet-name>resteasy-servlet</servlet-name> <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> </servlet> <servlet-mapping> <servlet-name>resteasy-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>Servletx</servlet-name> <servlet-class>org.allowed.bitarus.Servletx</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servletx</servlet-name> <url-pattern>/servletxxx</url-pattern> </servlet-mapping> </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 {{{#!highlight 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.allowed.bitarus</groupId> <artifactId>test2</artifactId> <version>0.0.1</version> <packaging>ejb</packaging> <build> <sourceDirectory>ejbModule</sourceDirectory> <resources> <resource> <directory>ejbModule</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <artifactId>maven-ejb-plugin</artifactId> <version>2.3</version> <configuration> <ejbVersion>3.0</ejbVersion> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <!-- EJB 3.0 --> <groupId>javax.ejb</groupId> <artifactId>ejb-api</artifactId> <version>3.0</version> </dependency> <dependency> <!--jax ws web servies --> <groupId>sun-jaxws</groupId> <artifactId>jsr181-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> </dependencies> </project> }}} Source code files in ejbModule/org/allowed/bitarus {{{#!highlight java //WSTest.java package org.allowed.bitarus; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import javax.ejb.Stateless; import javax.jws.WebMethod; import javax.jws.WebService; import javax.naming.Context; import javax.naming.InitialContext; /** * Session Bean implementation class WSTest */ @Stateless @WebService // http://localhost:8080/test2-0.0.1/WSTest?wsdl public class WSTest implements WSTestRemote, WSTestLocal { private static final String QUERY1 = "SELECT distinct table_name FROM user_tables"; private static final String JNDI_DATASOURCE = "java:OracleDS"; // private static final String QUERY1 = "select table_name from information_schema.tables;"; // private static final String JNDI_DATASOURCE = "java:/MysqlDS"; public WSTest() { } @WebMethod public String helloWorld() { return "Hello world"; } @WebMethod public String[] checkDB() { ArrayList<String> list = new ArrayList<String>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Context ctx = new InitialContext(); conn = ((javax.sql.DataSource) ctx.lookup(WSTest.JNDI_DATASOURCE)).getConnection(); ps = conn.prepareStatement(WSTest.QUERY1); rs = ps.executeQuery(); while (rs.next()) { list.add(rs.getString(1)); } } catch (Exception e) { list.add(e.getMessage()); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } String[] r = new String[list.size()]; list.toArray(r); return r; } } }}} {{{#!highlight java //WSTestLocal.java package org.allowed.bitarus; import javax.ejb.Local; @Local public interface WSTestLocal extends IWSTest{ } }}} {{{#!highlight java //WSTestRemote.java package org.allowed.bitarus; import javax.ejb.Remote; @Remote public interface WSTestRemote extends IWSTest{ } }}} {{{#!highlight java //IWSTest package org.allowed.bitarus; public interface IWSTest { public String helloWorld(); } }}} 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 Check deployed EJB with http://localhost:8080/test2-0.0.1/WSTest?wsdl File structure: {{{ vitor@darkstar$tree . . |-- ejbModule | `-- org | `-- allowed | `-- bitarus | |-- IWSTest.java | |-- WSTest.java | |-- WSTestLocal.java | `-- WSTestRemote.java |-- pom.xml `-- target |-- classes | `-- org | `-- allowed | `-- bitarus | |-- IWSTest.class | |-- WSTest.class | |-- WSTestLocal.class | `-- WSTestRemote.class |-- maven-archiver | `-- pom.properties |-- surefire `-- test2-0.0.1.jar 11 directories, 11 files }}} === Oracle datasource for JBoss 5 === Copy to <...>\jboss-5.1.0.GA\server\default\deploy\oracle-ds.xml Copy ojdbc6.jar to <...>\jboss-5.1.0.GA\server\default\lib {{{#!highlight xml <?xml version="1.0" encoding="UTF-8"?> <datasources> <!-- copy ojdbc6.jar to <...>\jboss-5.1.0.GA\server\default\lib --> <local-tx-datasource> <jndi-name>OracleDS</jndi-name> <connection-url>jdbc:oracle:thin:@localhost:1521:xe</connection-url> <driver-class>oracle.jdbc.driver.OracleDriver</driver-class> <user-name>SYSTEM</user-name> <password>jboss</password> <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name> <metadata> <type-mapping>Oracle9i</type-mapping> </metadata> </local-tx-datasource> </datasources> }}} === MySQL datasource for JBoss 5 === Copy to <...>\jboss-5.1.0.GA\server\default\deploy\mysql-ds.xml Copy mysql-connector-java-5.1.30-bin.jar to <...>\jboss-5.1.0.GA\server\default\lib {{{#!highlight xml <?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>MysqlDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/mysql</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>12345678</password> <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources> }}} == Assembly plugin == The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive. http://maven.apache.org/plugins/maven-assembly-plugin/usage.html {{{ <project> <!-- ... --> <build> <!-- ... --> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </project> }}} == Standalone app == 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.example</groupId> <artifactId>artifactX</artifactId> <packaging>jar</packaging> <version>0.0.1</version> <name>artifactX</name> <url>http://maven.apache.org</url> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.7</version> </dependency> </dependencies> </project> }}} Init script: {{{ #!/bin/sh # chkconfig: 345 99 99 # description: Starts and stops the progx process USER=userx PROG=progx PIDFILE="/tmp/progx.pid" PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin" LOG="/tmp/progx.log" JAVA="/usr/java/jdk1.6.0_26/bin/java" JARFILE="/home/userx/progx-0.0.1-jar-with-dependencies.jar" start(){ echo "Start called $PROG with user $USER" su - $USER -c "$JAVA -jar $JARFILE " & /bin/sleep 2 PID=`ps uax| grep java | grep $PROG | grep $USER | awk '{print $2}'` echo "Program $PROG running with PID: $PID" echo "$PID" > $PIDFILE } status(){ PID=`cat $PIDFILE` echo "Running $PROG with PID: $PID" } stop(){ echo "Stop called for $PROG" PID=`cat $PIDFILE` echo "PID to kill $PROG $PID" kill -9 $PID rm $PIDFILE } # switch case "$1" in start) start ;; status) status ;; stop) stop ;; restart) stop start ;; *) echo "$PROG start|status|stop|restart" exit 1 ;; esac }}} == Install JAR local repository == To install a JAR in the local repository use the following command: {{{ mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> }}} {{{ mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=C:\temp\test\tools.jar” }}} Dependency: {{{ <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.4.2</version> </dependency> }}} == Maven skip tests == http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html * mvn clean install -Dmaven.test.skip=true * mvn clean install -DskipTests or {{{#!highlight xml <project> <!-- [...] --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> <!-- [...] --> </project> }}} |
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
Sample WebApp file with source code to use MySQL data source:WebApp.zip
Sample WebAppJersey2, with dependencies for Jersey2 and JSON, to be deployed on Tomcat 7:WebAppJersey2.zip
Deploy web application on jboss AS 7.1.1
- /opt/jboss-as-7.1.1.Final/bin/standalone.sh
- admin 12345678
- Manage deployments
- Add content
- Choose file ~/WebApp/target/WebApp.war
- next, save
- enable , confirm
Add REST and servlet support
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 // File src/main/java/org/allowed/bitarus/Servletx.java
2 // References EJB in JBoss5. Test to see EJB invocation from Tomcat
3 package org.allowed.bitarus;
4
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import java.io.*;
8
9 import java.security.Security;
10 import java.util.Properties;
11 import javax.naming.Context;
12 import javax.naming.InitialContext;
13 import org.allowed.bitarus.IWSTest;
14
15 public class Servletx extends HttpServlet{
16 public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
17 {
18 res.setContentType("text/html");
19 PrintWriter out = res.getWriter();
20 String text="";
21 try{
22 Properties env = new Properties();
23 env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
24 env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
25 env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
26 InitialContext context = new InitialContext(env);
27 IWSTest test = (IWSTest) context.lookup("WSTest/remote");
28 text=test.helloWorld();
29 }
30 catch(Exception ex){
31 }
32 out.println("<html><head><title>Hello World!</title></head>");
33 out.println(String.format("<body><h1>Hello World! %s </h1></body></html>",text));
34 }
35 }
Dependency for JAR that has IWSTest interface that WSTest EJB implements:
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
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 // private static final String QUERY1 = "select table_name from information_schema.tables;";
26 // private static final String JNDI_DATASOURCE = "java:/MysqlDS";
27
28 public WSTest() {
29 }
30
31 @WebMethod
32 public String helloWorld() {
33 return "Hello world";
34 }
35
36 @WebMethod
37 public String[] checkDB() {
38 ArrayList<String> list = new ArrayList<String>();
39
40 Connection conn = null;
41 PreparedStatement ps = null;
42 ResultSet rs = null;
43
44 try {
45 Context ctx = new InitialContext();
46 conn = ((javax.sql.DataSource) ctx.lookup(WSTest.JNDI_DATASOURCE)).getConnection();
47 ps = conn.prepareStatement(WSTest.QUERY1);
48 rs = ps.executeQuery();
49 while (rs.next()) {
50 list.add(rs.getString(1));
51 }
52 } catch (Exception e) {
53 list.add(e.getMessage());
54 } finally {
55 if (rs != null) {
56 try {
57 rs.close();
58 } catch (SQLException e) {
59 e.printStackTrace();
60 }
61 }
62
63 if (ps != null) {
64 try {
65 ps.close();
66 } catch (SQLException e) {
67 e.printStackTrace();
68 }
69 }
70 if (conn != null) {
71 try {
72 conn.close();
73 } catch (SQLException e) {
74 e.printStackTrace();
75 }
76 }
77 }
78
79 String[] r = new String[list.size()];
80 list.toArray(r);
81 return r;
82 }
83 }
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
Check deployed EJB with http://localhost:8080/test2-0.0.1/WSTest?wsdl
File structure:
vitor@darkstar$tree . . |-- ejbModule | `-- org | `-- allowed | `-- bitarus | |-- IWSTest.java | |-- WSTest.java | |-- WSTestLocal.java | `-- WSTestRemote.java |-- pom.xml `-- target |-- classes | `-- org | `-- allowed | `-- bitarus | |-- IWSTest.class | |-- WSTest.class | |-- WSTestLocal.class | `-- WSTestRemote.class |-- maven-archiver | `-- pom.properties |-- surefire `-- test2-0.0.1.jar 11 directories, 11 files
Oracle datasource for JBoss 5
Copy to <...>\jboss-5.1.0.GA\server\default\deploy\oracle-ds.xml
Copy ojdbc6.jar to <...>\jboss-5.1.0.GA\server\default\lib
1 <?xml version="1.0" encoding="UTF-8"?>
2 <datasources>
3 <!-- copy ojdbc6.jar to <...>\jboss-5.1.0.GA\server\default\lib -->
4 <local-tx-datasource>
5 <jndi-name>OracleDS</jndi-name>
6 <connection-url>jdbc:oracle:thin:@localhost:1521:xe</connection-url>
7 <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
8 <user-name>SYSTEM</user-name>
9 <password>jboss</password>
10 <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name>
11 <metadata>
12 <type-mapping>Oracle9i</type-mapping>
13 </metadata>
14 </local-tx-datasource>
15 </datasources>
MySQL datasource for JBoss 5
Copy to <...>\jboss-5.1.0.GA\server\default\deploy\mysql-ds.xml
Copy mysql-connector-java-5.1.30-bin.jar to <...>\jboss-5.1.0.GA\server\default\lib
1 <?xml version="1.0" encoding="UTF-8"?>
2 <datasources>
3 <local-tx-datasource>
4 <jndi-name>MysqlDS</jndi-name>
5 <connection-url>jdbc:mysql://localhost:3306/mysql</connection-url>
6 <driver-class>com.mysql.jdbc.Driver</driver-class>
7 <user-name>root</user-name>
8 <password>12345678</password>
9 <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
10 <metadata>
11 <type-mapping>mySQL</type-mapping>
12 </metadata>
13 </local-tx-datasource>
14 </datasources>
Assembly plugin
The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
<project> <!-- ... --> <build> <!-- ... --> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </project>
Standalone app
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.example</groupId> <artifactId>artifactX</artifactId> <packaging>jar</packaging> <version>0.0.1</version> <name>artifactX</name> <url>http://maven.apache.org</url> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.7</version> </dependency> </dependencies> </project>
Init script:
# chkconfig: 345 99 99 # description: Starts and stops the progx process USER=userx PROG=progx PIDFILE="/tmp/progx.pid" PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin" LOG="/tmp/progx.log" JAVA="/usr/java/jdk1.6.0_26/bin/java" JARFILE="/home/userx/progx-0.0.1-jar-with-dependencies.jar" start(){ echo "Start called $PROG with user $USER" su - $USER -c "$JAVA -jar $JARFILE " & /bin/sleep 2 PID=`ps uax| grep java | grep $PROG | grep $USER | awk '{print $2}'` echo "Program $PROG running with PID: $PID" echo "$PID" > $PIDFILE } status(){ PID=`cat $PIDFILE` echo "Running $PROG with PID: $PID" } stop(){ echo "Stop called for $PROG" PID=`cat $PIDFILE` echo "PID to kill $PROG $PID" kill -9 $PID rm $PIDFILE } # switch case "$1" in start) start ;; status) status ;; stop) stop ;; restart) stop start ;; *) echo "$PROG start|status|stop|restart" exit 1 ;; esac
Install JAR local repository
To install a JAR in the local repository use the following command:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=C:\temp\test\tools.jar”
Dependency:
<dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.4.2</version> </dependency>
Maven skip tests
http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html
- mvn clean install -Dmaven.test.skip=true
mvn clean install -DskipTests
or
1 <project>
2 <!-- [...] -->
3 <build>
4 <plugins>
5 <plugin>
6 <groupId>org.apache.maven.plugins</groupId>
7 <artifactId>maven-surefire-plugin</artifactId>
8 <version>2.18.1</version>
9 <configuration>
10 <skipTests>true</skipTests>
11 </configuration>
12 </plugin>
13 </plugins>
14 </build>
15 <!-- [...] -->
16 </project>