Maven packaging for websphere - java

I am trying to build an EAR file - Which can be deployed in IBM websphere server.
This is an existing struts appliation, i am trying to mavenize it.
This project contains two folders
1. web
2. webEAR
web is actually for war file and webEAR folder for the EAR file, web contains all the code, and webEAR is a kind of a wrapper.
Steps I have already done are below
IDE - Eclipse
Java version - 1.7
Convereted both web and webEAR to Maven - (Configure to Maven)
edited the POM.XML like below
<modelVersion>4.0.0</modelVersion>
<groupId>com.comp.web</groupId>
<artifactId>web</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>WEB</name>
<description>WEB</description>
added all relevant jar files - which are in lib folder as below (sample)
<dependency>
<groupId>jarfile</groupId>
<artifactId>com.ibm.jar</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEBINF/lib/com.ibm.jarfile.jar</systemPat>
</dependency>
Now i dont have any errors in the eclipse, and I can run the application by right clicking the webEAR folder -> Run in Server, It works.
but I am not sure, how to create a EAR file , which has the war file, so that I can deploy in the WAS server dev environment.
Can someone show me a way I can do this. currently there is no POM.xml in the webEAR maven folder
P.S - I am not a Java developer. This is a first maven related project I am assigned to. I appreciate any help

Your module should have <packaging>ear</packaging>.
In the dependencies for this ear module ( Use a new module to build the ear ) include your war module as below.
<dependency>
<groupId>com.comp.webGroupId</groupId>
<artifactId>war-artifact</artifactId>
<version> war-version</version>
<type>war</type>
</dependency>
In the build plugins for this ear module include the maven-ear-plugin.
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<finalName>web</finalName>
<version>versionNumber</version>
<generatedDescriptorLocation>${basedir}/src/main/application/META-INF</generatedDescriptorLocation>
<modules>
<webModule>
<groupId>com.comp.webGroupId</groupId>
<artifactId>war-artifact</artifactId>
<uri>web.war</uri>
<bundleFileName>web.war</bundleFileName>
<contextRoot>/applicationName</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
Add any specific configuration values as required.

Related

How to create valid war file from /src & /WebContent folder in CLI?

I have a project with 2 folders:
1) src - contains the Java code files & folders (entity, code, rest,
timer)
2) WebContent - contains folders (META-INF, WEB-INF)
I have not experienced with server-side Java and I would like to ask how I can create a valid .war file using CLI that can be deployed using the Wildfly web admin interface and achieve that project is available and running on URL defined in web.xml file (located in WEB-INF folder).
I tried to deploy war which been created using the following command in root folder (i tried WebContent folder too) but after uploading the .war file the application is not reachable on the given URL (404).
Thanks for any advice.
Edit:
Added actual project structure
I'd encourage you to move to a build tool of some sort. There are various options - I'll go over Maven as it is the one I'm most familiar with.
First thing is code organization. You'll need to organize your code like:
src/
main/
java/
com/
packagename/
SomeJavaCode.java
webapp/
WEB-INF/
web.xml
So all of your Java code will be somewhere under src/main/java in whatever packages you want. In src/main/webapp are files related to your web application. You should have a minimum of WEB-INF/web.xml for a .war file but if you have a .jsp or .html it would go in the webapp directory too.
At the root of the directory you will need a pom.xml file. This is your build file that tells Maven what to do. A super basic one for a .war file would be:
<?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>
<groupId>tld.companyname</groupId>
<artifactId>your-artifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
This file only has dependencies on the JEE stack with the Servlet libraries. These are both marked as "provided" as you don't need to include these in your .war file as Wildfly already provides them.
Note that you are very likely to have additional dependencies for your code. They need to be added as a dependency stanza in the pom.xml like the other ones. If you're not sure what this value should be go to mvnrepository.com to search for the correct code to insert.
The output of this will be a .war file in the target directory. This .war file can be deployed to Wildfly.
An extra piece - to help Wildfly determine the root URL path of your .war file, include the file jboss-web.xml in your WEB-INF directory:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web version="10.0"
xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_10_0.xsd">
<context-root>/myapp</context-root>
</jboss-web>
When you deploy this .war the jboss-web.xml file will tell Wildfly to put it under the path /myapp in this example. To me this removes some of the confusion around automatic web app names that can occur. Note that it is perfectly legitimate to have the context-root be / so that your .war is at the root of the URL hierarchy.
Once you have all that, you'll need to install Maven and have it on your PATH. Take a look at the Maven Install page for details for your O/S. After this is installed just run:
mvn clean package
on the command line to generate the .war in your target directory.
EDIT
One thing that is different is that you do not need to add the dependencies inside of your code - Maven takes care of that. So, for example, in your image for the AWS libraries I would just add:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.119</version>
</dependency>
inside of the dependencies block. Maven will automatically pull this library from the Maven central repository and use it. You don't need to have it in your WEB-INF/lib directory as, when Maven builds, it will automatically include the libries you have in your dependencies (and anything they depend on). Again, just search in https://mvnrepository.com/ for the library you need.

Deploy dynamically project on Tomcat from eclipse

I am wondering if there is a way to dynamically deploy a maven project on eclipse and have the same result as if it was packaged in a WAR.
I noticed that when I right-click the project and choose "run as" and then choose tomcat the project deployed does not work properly but when I generate the war and place it manually it works fine.
That means everytime I change something in the code I have to generate a war and deploy it manually on the server.
Is there a tomcat config that I can use to have an output when I run from eclipse similar to when i generate a war?
I'm using tomcat 6.0.26 and eclipse Neon 3.
Thanks.
Yes, you can ...
In general the steps are:
1) Turn your existing project into a maven project by adding a pom.xml like this into the project root folder:
<?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>
<groupId>com.yourcompany</groupId>
<artifactId>yourApp</artifactId>
<packaging>war</packaging>
<version>2.3</version>
<name>Your Web Application</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Add your dependencies as needed. -->
</dependencies>
</project>
2) Rightclick the project and Configure -> Convert to Maven Project
3) In Eclipse Servers view add an Apache Tomcat.
4) Rightclick the Tomcat server and Add and Remove ... -> Add your project.
5) Launch the Tomcat in debug mode.
Now eclipse will deploy the project in exploded .WAR format, replacing and therefore hot deploying resources on every change and compiling Java classes on change.
If you do not alter Java method or class signature, changes in Java source code and resources take effect instantly without the requirement to restart Tomcat or the web application.
The problem was that I didn't configure the maven profiles when I ran the project from eclipse which explains why I had different outputs when deploying the project from a war and directly from eclipse. The answer here Maven Profiles and Tomcat in Eclipse explains how to configure maven profiles when you run on server from eclipse.

Generated .ear.xml for Liberty in Eclipse is wrong

Problematic plugin
I have an issue with the eclipse Liberty plugin which is available here on the marketplace. I linked the beta, but the behaviour with stable is exaclty the same.
Maven ear project description
I have a maven project which consists of multiple maven modules. The ear module's pom.xml looks like this. By the way, the maven generated ear looks just fine.
<?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>
<artifactId>myear</artifactId>
<packaging>ear</packaging>
<name>my ear module</name>
<dependencies>
<dependency>
<groupId>my.groupid</groupId>
<artifactId>myWARmodule</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>compile</scope>
<!-- EJBs are not to be included in the war. -->
<exclusion>
<groupId>my.groupid</groupId>
<artifactId>myEJBmodule</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>my.groupid</groupId>
<artifactId>myWARmodule</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
<exclusions>
<exclusion>
<groupId>my.groupid</groupId>
<artifactId>myEJBmodule</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- jar module -->
<dependency>
<groupId>my.groupid</groupId>
<artifactId>myAdditionalJarModule</artifactId>
<version>${project.version}</version>
<type>jar</type>
</dependency>
<!-- EJB module -->
<dependency>
<groupId>my.groupid</groupId>
<artifactId>myEJBmodule</artifactId>
<type>ejb</type>
</dependency>
<!-- API implementation -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.jsr107.ri</groupId>
<artifactId>cache-annotations-ri-cdi</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>myEarApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<version>7</version>
<finalName>myEARApp</finalName>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<defaultJavaBundleDir>lib</defaultJavaBundleDir>
<skinnyWars>true</skinnyWars>
<modules>
<!-- Root level modules: WAR and EJBs. -->
<webModule>
<groupId>my.groupd</groupId>
<artifactId>myWARmodule</artifactId>
</webModule>
<ejbModule>
<groupId>my.groupid</groupId>
<artifactId>myEJBmodule</artifactId>
</ejbModule>
<jarModule>
<groupId>my.groupid</groupId>
<artifactId>myAdditionalJarModule</artifactId>
<includeInApplicationXml>true</includeInApplicationXml>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
Resulting ear
The resulting ear file is structured like this:
myEARApp.ear
- myWarApp.war
-- (no libs in WEB-INF/lib)
- lib/
-- (a lot of libs here due to skinny wars)
- myEJBmodule.jar
- myAdditionalJarModule.jar
Properties - Maven assembly descriptor
When I click the ear project and look at "assembly descriptor", the assembly matches the layout of the ear file, which correct.
So, this is fine.
Creating a liberty server and adding the application
Now, when I add in eclipse a server and want to run the ear file, I create a "websphere liberty profile server", version 17.0.0.2. I then add the ear module to the server, configure the application (i.e. application bindings etc.).
The server can run the project from workspace by generating a replacement myEARApp.ear.xml file in the server/apps folder. It has the structure of the ear file. Well, it should have. Since two weeks or so, the structure of the myEARApp.ear.xml-file is more like this:
myEARApp.ear
- myWarApp.war
-- a lot of libs from the war module
-- myEJBmodule.jar
-- myAdditionalJarModule.jar
- lib/
-- only a few extra jar modules referenced in ear/pom.xml
-- missing: logback, ehcache, etc.
- myEJBmodule.jar
- myAdditionalJarModule.jar
- otherDependencyOfmyAdditionalJarModule.jar
The bad thing is that this obivously won't run due to duplicate beans available for injection and a lot classnotfoundexceptions.
How to solve?
So, my question is: How does the eclipse liberty profile plugin generate it's .ear.xml-file? Why does it differ from the layout I configured in the pom.xml? Even without skinny wars and the import scope dependency, the generated myEARApp.ear.xml file for liberty won't change.
Help appreciated!
found the solution.
I removed a lot of jboss plugins from eclipse, like JBoss developer tools, JBoss Hibernate, etc.
I just kept a few:
CDI tools
Java EE Batch Config Tools
JMX Console
M2E connector for Eclipse JDT Compiler
Maven Integration for Eclipse JDT APT
Now it works just fine again. Who would have thought that? Also, it didn't appear in any log why the generation of the .ear.xml file for liberty was corrupted by JBoss Dev Studio or other plugins.
I hope this is useful to any other developers.
WDT generates the myEARApp.ear.xml file based on the deployment assembly settings in the project properties. When the project is imported into the workspace, the Eclipse M2E plugin is supposed to interpret the pom.xml and translate the settings and setup the deployment assembly in the project properties based on the settings in the pom.xml file.
Can you check the Deployment Assembly page in the project properties of the EAR and Web modules to see if the module is being setup properly as you would expect? The other thing that you can try is to export the EAR file (using the Export menu when you right click on the EAR project). If the exported project does not have the structure that you expect, then the problem is caused by the Eclipse M2E plugin does not support skinnyWars properly as mentioned in the bugzilla on the earlier comment.
Looks like Eclipse M2E plugin doesn't support skinnyWars. You can try the work around at the bottom of the bug report.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=461613

Use maven to pack modules into an ear module

I have a project divided into modules/sub-projects.
Root
- module jar (used to contain ejb)
- module war (used to contain web app)
- module ear (used to pack the two aforementioned modules into one deployable unit)
Without using maven we should:
Build the jar and war projects separately and then
pack them in a ear.
I'm new to Maven but in my understanding Maven is used to manage this process
on behalf of the developer.
My project in term of maven is structured in the following way:
ROOT
parent-pom.xml
- jar-pom.xml
- war-pom.xml
- ear-pom.xml
My question is how do we tell maven to pack the jar and war module into a .ear
project, wich pom.xml should host this information ?
Edit:
The following is my project structure
Find the sample pom.xml under ear project to build ear
Add
<packaging>ear</packaging>
Add Dependencies
<dependencies>
<dependency>
<groupId><Your api Jar Group Id></groupId>
<artifactId><Your API artifact Id></artifactId>
<version>${project.parent.version}</version>
<type>ejb</type>
</dependency>
Add build plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId
<configuration>
<version>6</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<ejbModule>
<groupId><Your EJB Module Group Id></groupId>
<artifactId><Your EJB Module Artifact Id></artifactId>
</ejbModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>

Maven Tomcat7 run a dependency war

I have a question regarding maven and its tomcat7 plugin :)
I have the following maven projects:
plugin1: plain java project packaged as jar
plugin2: plain java project packaged as jar
webapp: standalone webapp project packaged as jar
those three project are properly build in maven and the outcome works fine:
I can use the jars from plugin1/plugin2
I can deploy the webapp war file to a web container
I can run tomcat7:run to start the webapp
Now, I need to provide different packaging of the webapp containing specific plugin setup.
i.e. I want to generate a war file with webapp + plugin1 and another one with webapp + pugin2
To achieve this, I have created 2 additionnal maven projects that declare dependancies on the webapp project + the appropriate plugin projects and are packaged as wars.
The generated war files have the expected content, and can be deployed to a tomcat, but when I try to use the maven tomcat plugin (tomcat7:run again), it simply doesnt start anything.
Though this is not blocking for me (my main point was to generate the war files), I have the feeling that I missed something.
the pom.xml for those aggregate projects looks like this (note that there is absolutly no code in those projects, these were just created for packaging with specific dependancies convenience).
<groupId>my.project</groupId>
<artifactId>live1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>MyWebapp</name>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>my.project</groupId>
<artifactId>plugin1</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>my.project</groupId>
<artifactId>webapp</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<finalName>MyWebapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<url>http://localhost:8080/manager</url>
<server>localhost</server>
<path>/${project.build.finalName}</path>
</configuration>
</plugin>
</plugins>
</build>
Thanks !
note: long time lurker, first time asker here, if some information is missing tell me :)
Depending on the structure of your project it may not be suficient to just add a dependency of type war. You may need also to configure <overlays> as described here maven-war-plugin.
It looks like your final war does not provide the full web configuration that you expect. With overlays you can configure how the resources from the dependency will be packed into your final web app.
There must be some difference in the way that your external tomcat starts the app compared to the tomcat7 plugin. May be you can try -X option :
mvn -X tomcat7:run
This should log out some details, of what the embedded tomcat is configuring..

Categories

Resources