How to create a WAR file using the commandline? - maven

I installed JBoss Developer Studio and it's possible to create a WAR file by "right mouse project > Export > WAR file" but I want to export my project to a WAR file using the commandline.
I have maven installed which is one of the requirements of the Studio and I read that I can generate a WAR file using maven but I require a file called pom.xml. When I searched through my workspace and project, pom.xml was missing. I may need to manually create pom.xml but I'm unsure how to.
My directory tree for my project is the following:
Siesta
├── build
│   └── classes
├── src
└── WebContent
├── extjs
├── extjs-4.2.0
├── extjs-4.2.2
├── index.jsp
├── META-INF
├── siesta
├── tests
└── WEB-INF
How do I create a WAR file for my Maven / JBoss project using the command line? I use Linux and would prefer not having to create a pom.xml file but if there isn't another way, then I'll use the xml file to generate the war file.
Edit:
So jar is the way to go to create a war file. I wrote up a tiny script that will create a war file for me for a specific directory.
#!/bin/bash
cd Siesta/WebContent/
jar -cvf ../../Siesta.war *
cd -
Then if you open the war file in a zip utility or archive manager in ubuntu, you'll see this structure
├── extjs
├── extjs-4.2.0
├── extjs-4.2.2
├── index.jsp
├── META-INF
├── siesta
├── tests
└── WEB-INF
I have to CD into the directory that I want to create a war file of which is kind of annoying. I think there may be a better way to do it using jar's -C option but when I used "jar -cvf Siesta.war -C Siesta/WebContent *" it didn't have the same result.
Edit2:
jar -cvf my-app.war myfolder/
For my application to work on TomCat, I use the following:
cd Siesta/WebContent
jar -cvf Siesta.war *

If this is a Maven project, then open your console, navigate to where the pom.xml file is and then run:
mvn package
It will create the {artifactId}-{version}.war file under ${basedir}/target directory. This assumes that your packaging POM element is set to war in your pom.xml file.
<project ...>
...
<packaging>war</packaging>
...
</project>
Some of the valid packaging values are jar, war, ear and pom. If no packaging value has been specified, it will default to jar.
More details here and here.

Alright here is a already existing SO with a couple of possibilities on how to create a war file: How to create war files.
I personaly prefer Maven over any other build scenario - Since i migrated from ANT i did not like Maven in the first place but after investing like a day or two into the documentations: http://maven.apache.org/pom.html i started to love it. In your case all you need is refactor your project to meet the maven standard file structure and create a minimum Maven project (a.k.a. pom.xml file) for your sources that has the packaging war instead of the default jar.
If you want to stick with the console only and dont use any build helpers beside the ones delivered by the JDK you can create your war with something like this:
jar cvf my-app.war
Have a look at this: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for a complete reference of the supported options.
(Note: A "war" in the context of java based web applications follows a standard format: http://docs.oracle.com/javaee/6/tutorial/doc/bnaby.html while the technical ".war" file is just the same as a ZIP compressed folder renamed to ".zip".)
(Also note i would realy take the time to get in touch with maven since after that you know about a serious build tool and will be able to create jar, ear, war etc. in a standard process).

The simplest choice (if you are using Maven) is to create it with the maven-archetype-webapp:
mvn archetype:generate -DgroupId=com.sample -DartifactId=web-project -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
You can then refine your pom.xml to include the dependencies required, for example:
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>8.0.0</version>
<scope>provided</scope>
</dependency>
Source: How to create a Web application from the Command Line

Related

maven release with multi module project and main pom in sub-directory

I'm working on a project, which originally was set up by a different author. Each module has its own directory. Also the main pom (my-project-pom), which includes the modules, is stored in a separate sub-directory. We're using subversion:
+-- branches
+-- tags
'-- trunk
+-- my-project-pom
+-- my-project-web
+-- my-project-ear
'-- README
Now I'm trying to setup the maven release plugin with following goals:
Everything is included in the commit from maven release, also changes in the root README file
SVN tags are automatically created in the proper repository directory tags\<version>
So my questions are:
Is this possible at all, although the main pom is not in the root directory?
How should I configure the <connection> and <developerConnection> URLs in my-project-pom to achieve this? Do I have to point directly to the trunk or to trunk\my-project-pom or something different
Is there any other configuration required besides the <scm> configuration above and the definition of the plugin dependency - e.g. the tagBase setting?
In which directory do I have to execute the mvn release commands?

How to place context.xml in war's META-INF?

I am building a webapp through Gradle's war plugin. In order to disable Tomcat's session persistence, I need to place the file context.xml in the META-INF directory of the war's root.
I attempted the following:
Create the file src/main/webapp/META-INF/context.xml of the main project
Create the file src/main/resources/META-INF/context.xml of the main project
However, when I build the project using gradle clean war, the produced war file contains a META-INF with only a MANIFEST.MF in it. It is as if my directory gets overridden.
How do I place context.xml in the war?
src/main/webapp/META-INF/context.xml is correct and works fine for me. Chances are that you went wrong somewhere, or that there is a problem with your build script.

Jenkins + Sonar + Maven with poms in extra directory structure

I have a (big) maven project with many modules which looks like this at the top
src/
maven-build/
src contains the tree with all the Java sources. maven-build contains the tree with all the poms
I can build everything fine with Jenkins, but when I add a Sonar analysis triggered through maven, Sonar complains that
[...]/src/x/y/Z.java is not in basedir [...]/maven-build
I already tried copying the Java files to the maven-build directory, but this doesn't help...
Is there a way to change the basedir on Sonar? Or can I solve this in a different way?
I copy my root-pom to the directory containing the source-directories. You could also try copying the pom from maven-build to the directory containing "src". In that way, "[...]/src/x/y/Z.java" would be within basedir.
pom.xml
src/
maven-build/

Building .ear and .war with maven

I am generating a maven archetype using the following command "mvn archetype:generate -DgroupId=com.mycompany.core -DartifactId=myproject-core -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false", which generates the war file, now I want to build it using ear file.
I have gone through the web and some posts here that two different archetypes need to created for ear and war files. But I am not getting the starting point here.
Do I need the same archetype command which I am using to generate the structure or do I need to change the parameters? Any help would be greatly appreciated.
THANK YOU
I would suggest to use the correct archetypeArtifactId=maven-archetype-j2ee-simple instead of webapp cause EAR means Java EE application which usually contains at least two modules like:
+-- root(pom.xml)
+-- module-war (pom.xml)
+-- module-ear (pom.xml)

Packaging multi-platform multi-module maven project

I'm participating in an open source project (ps3mediaserver) which has been moved from google code (SVN) and ANT (for build tasks) to git (GitHub) and maven. I've got my own fork (called pms-mlx), where I'd like to maintain some plugins being part of the default packaging when releasing. I'm pretty new to maven and am not too sure how the project should be structured to respect the maven way.
I'll start by describing how the environment behaved previously and will then give the thoughts about the move to maven.
Links:
Old: SVN + ANT ps3mediaserver project on google code
Old: SVN + ANT pms-mlx project on SourceForge
New: Git + Maven ps3mediaserver project on GitHub
New: Git + Maven pms-mlx project on GitHub
Old behavior:
Project structure:
+--workspace
+--plugins
+--plugin1
build.xml
+--plugin2
build.xml
+--ps3mediaserver_mlx
+--plugins
build.xml
The main project is ps3mediaserver_mlx, all plugins live in sub-folders of the workspace/plugins folder.
ps3mediaserver_mlx/build.xml contains a target BuildWithoutLibs which will build the jar of the main project and copy it to workspace/pms_no_libs.jar which will then be referenced (at this location) by the plugins.
When executing the build target of any plugin, the plugin will be build and the resulting jar copied to ps3mediaserver_mlx/plugins/[plugin_name].jar.
And finally, when packaging the application using the the build target in ps3mediaserver_mlx/build.xml, the plugins containedin workspace/ps3mediaserver_mlx/plugins will be packaged (in a exe installer for windows, dmg for OSX or tar.gz for linux).
New behavior
The project structure has been changed to this:
+-- workspace/
+-- pom.xml (global-pom)
+-- ps3mediaserver/
| +-- pom.xml (pms-pom)
| +-- src/
| ...
+-- plugins/
| +-- pom.xml (plugins-pom)
| +-- Plugin1/
| pom.xml (plugin1-pom)
| src/
| +-- Plugin2/
| pom.xml (plugin2-pom)
| src/
+-- pms-package/
+-- pom.xml (package-pom)
+-- src/main/assembly/
+-- src/main/external-resources/
Responsabilities:
global-pom The root pom containing all dependencies used by pms. This lets use the same version without redeclaring them in any plugin (is this a good idea?). Builds everything and contains a modules section to perform the same maven commands on all projects
<modules>
<module>ps3mediaserver</module>
<module>plugins</module>
<module>pms-package</module>
</modules>
pms-pom: Inherits from global-pom andbuilds the pms jar
plugins-pom: Inherits from global-pom; contains a depency for pms (which will be required for all plugins); contains a list of all modules having to be built
pluginX-pom: Inherits from plugins-pom and contains a custom configuration for a plugin
package-pom: Is responsible to package pms according to the platform it is being built on.
Does this structure represent the way maven is ment to be used?
Everything is working up to the packaging. This means the main application jar as well as all the plugins have been built and need to be packaged. The package-pom is responsible to do that.
In the original application there is only one pom.xml and the packaging is being done by using different profiles for Windows, Linux and OS X. The one I'm currently working on is for OSX and uses osxappbundle-maven-plugin, but the source code is never being packaged in the app file. That's because the packaging project doesn't inherit from the actual project anymore.
How has the built jar to be referenced in order to be packaged correctly in the app file?
I've tried referencing the jar in additionalResources and as custom class path, but never with success.
You have defined a dependency for example in the plugins/pom.xml
<dependencies>
<dependency>
<groupId>net.pms</groupId>
<artifactId>pms-mlx</artifactId>
<version>1.52.1_mlx_v0.8-SNAPSHOT</version>
</dependency>
</dependencies>
which exactly represents your parent. In other words it's wrong to define a dependency which already been defined as your parent.
It's good practice to put the modelVersion tag directly after the project tag and before the parent tag. After the parent tag put the information the current module like artifactId.
After diving into the project i noticed that you defined in your of your plugins/WebservicePlugin:
<modelVersion>4.0.0</modelVersion>
<artifactId>WebservicePlugin</artifactId>
<version>3-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>net.pms</groupId>
<artifactId>pms-plugins</artifactId>
<version>1.52.1_mlx_v0.8-SNAPSHOT</version>
</parent>
which is against the maven way for multi-module builds. You should not define a different version in this case. It should look like this:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.pms</groupId>
<artifactId>pms-plugins</artifactId>
<version>1.52.1_mlx_v0.8-SNAPSHOT</version>
</parent>
<artifactId>WebservicePlugin</artifactId>
If you have problems based on the version of the WebservicePlugin module than you should think about separating the WebservicePlugin from the rest (may be the other plugins as well).
One other things which i have noticed that you defined in many of the plugins (if not all of them) the configuration and the usage of the maven-compiler-plugin...This should be done by using a pluginManagement part in your root pom...to simplyfy maintenance of your project.
The copying of the created plugins-jars via the maven-antrun plugin into a different location whould be done different.
Repeating the license entry in every plugin is not needed cause it's inherited by the parent.

Resources