how can i package spring mvc application to jar? - java

I have an application built using maven and spring mvc and tomcat7-maven-plugin. Can I package these to a jar file, so that i can run the jar file to start a tomcat server?
That means i don't need tomcat, only a jar file can provide a web service.

Spring documentation is your friend. I am providing the links which you can follow, with highlighted parts in this post.
Spring Boot Maven Packaging
Once spring-boot-maven-plugin has been included in your pom.xml it will automatically attempt to rewrite archives to make them executable using the spring-boot:repackage goal. You should configure your project to build a jar or war (as appropriate) using the usual packaging element in the pom.xml
The main class that you want to launch can either be specified using a configuration option, or by adding a Main-Class attribute to the manifest in the usual way.
To build and run a project artifact, you can type the following:
$ mvn package
$ java -jar target/mymodule-0.0.1-SNAPSHOT.jar
More information about the plugin is available here

Related

Running Standalone Jar need to add jar on server to run

I created a spring boot job which relies on properties on the server and I can get it to run like so, no modifying manifest.
/bin/java -Dspring.config.location=/var/tmp/com.jdbc.properties -jar my.jar
and it works. But the application relies upon another jar that is an internal jar that lives under /usr/local/share/jni/foo.jar which I want to add to this mix.
I have tried countless runs trying such things as:
java -cp /usr/local/share/jni/foo.jar -Dspring(picking up original line)
When I start to google this, it takes me on magical tours of running:
'org.springframework.boot.loader.JarLauncher'
or
'org.springframework.boot.loader.PropertiesLauncher'
then mucking with manifest etc.
Spent last 4 hours with no success. Is there a best practice to run a standalone jar that needs to consume remote properties file and an additional jar file? Would like to keep it simple if possible.
If you are using Spring Boot and want to have a Fat-jar that encapsulates all your dependencies, the best way is to add the required Jar as a dependency to your project.
Assuming you are using Maven to build your project, the "foo.jar" needs to be added as a Maven dependency to your project. Then, spring Boot maven plugin will pick up the jar and includes it in your Fat-jar.
Even if the "foo.jar" does not exist in any Maven repo, you still can add it manually to your local Maven repo using the Maven command mvn install:install-file (See Maven doc).
Did you try using foo.jar as a provided dependency within your maven/gradle dependencies and building the project as executable war file?
See spring boot's maven plugin description of building executable war files.
Overall. Run spring boot standalone jar on a Linux server. Additionally read the database properties from a static file on the server, and path in a jar file that adds functionality that only lives on the server. Cannot include in the boot lib.
command line run (will convert to shell) and ran.
/path/to/..openjdk-1.7.0.55.x86_64/bin/java -cp /usr/somewhere/jni/Foo.jar:/path/where/lib/MYBOOTJAR.jar org.springframework.boot.loader.JarLauncher --spring.config.location=/path/to/properties/on/server/com.xxx.yyy.zzz.jdbc.properties
Seems like using the JarLauncher (no modifications to manifest, except excluding the Foo.jar from local)
Hope this helps someone else.

Deploy jar with embedded jetty with maven

I have a (large) maven-web-project with many dependencies. The project runs an embedded jetty.
For some (customer related) reasons I cannot package a war to deploy it to some application server like a standalone jetty or tomcat.
Now I need to deploy the whole project with all dependencies-jars and the web-root stuff to some production-server, where I can start by command line the embedded jetty like java -jar myproject.jar.
How can I build such a package with maven. I tried maven-assembly-plugin, but I do not want to re-pack jars. (IMHO this is not a really good idea.)
I need to package some zip or tar like:
myproject.zip
--myproject.jar
--META-INF (with the whole classpath and may be the auto-start class)
--libs
--slf4j.jar
--logback.jar
--hibernate.jar
--and many, many more.
Any ideas? TIA!
If you use Spring you can use Spring Boot which does this by default. By using Spring Boot maven plugin it will build you a uber jar containing all your dependencies and an embed tomcat or jetty.

Maven to Eclipse build issue while creating web application

I'm trying to build web Service using maven and eclipse. Here is steps I followed.
Generated mvn folder struture using comman prompt
mvn archetype:generate - DarchetypeArtifactId=maven-archetype-webapp
Converted the mvn project into eclipse.
mvn eclipse:eclipse -Dwtpversion=2.0
Imported the project into my workspace.
Changed the project facets such as java version, servlet version, added cxf and jaxb feature and server runtime.
Copied the WSDL to Resources folder.
Since I do not want to add dependencies(as somebody else would be doing this job for me). I added spring and CXF lib into build path. and also to deployment assembly.
Generated the jaxb classes and operations from wsdl. Here all the java classes created instead of going to src/main/java to went to src/main/resources.
I run the app on tomcat. web.xml is invoked from that cxf framework got invoked and also spring bean creation got invoked. but while creating the bean for the webservice class it threw Class not found error.
When I opened up the war I could notice that while packaging instead of placeing the class files, the eclipse placed java file as it is in the war file.
Could some one help me in fixing this problem. Not sure how to configure eclipse to compile and place .class files in the war instead of .java filee.
Figured out the problem, while importing the file eclipse is adding command, exclude all the java files from src/main/resources in srource tab of java build path On removing it worked fine

Difference between web projects with pom.xml and web.xml

What is the difference between Java projects having pom.xml and web.xml? Can projects have both these configurations at the same time?
They're completely compatible. As a matter of fact, they perform completely unrelated tasks.
pom.xml is the configuration file for Maven projects. One of its goals is to provide assistance in the compilation and building of a project when using Maven. You can think of it as an ant build.xml file or a makefile Make file if you're not familiar to Maven (actually, it can provide a lot more functionality)
web.xml is the Java EE web application deployment descriptor, where you specify for instance servlets, servlet mappings and other aspects of a webapp.
What is Maven from the Apache Maven site.
What is web.xml file and what all things can I do with it? question on SO.
The two files have nothing to do with each other.
pom.xml - Maven configuration file. Controls the build process for
the project
web.xml - Web application configuration file. Controls the deployment
and configuration of the web application
The POM file really shouldn't be deployed with the application, its just for the build process.
web.xml is an indicator that the project is running in some kind of servlet container (possibly even a full-fledged Java EE container).
pom.xml is an indicator that the project is built using the Maven build system.
Those two things are entirely orthogonal, so any given project can have none, one or both of them.
The Pom defines any dependancy libraries, it is part of Maven. This tells maven what jar files to download and store in the lib folder of your site.
Web xml is how your web project is configured.
They can both coexist as they do different things.
POM stands for "Project Object Model". It is an XML representation of a Maven project held in a file named pom.xml. http://maven.apache.org/pom.html
yes you can have both configurations at the same time.
The pom.xml is for configure your project with Maven.
The web.xml is use in all Java EE project under Tomcat for example.
You can use both, Maven is for compile and deploy your project, Tomcat is your server.

Maven and autobuilding other Eclipse projects to your web application

I have made my own frameworks, and I use these frameworks in my web applications. Now I have to create JAR file for my project and then copy this jar to web application lib path. Is it possible to make Maven do this for me.
(Currently I don't use Maven, because I didn't get it to work with GAE.)
Yes of course, this is possible, any build tool can do that (even a shell script).
So in maven, you define a module for your framework. You define another module for your webapp, configured as a web app, add a dependency between them. This dependancy is enough for maven to understand that your framework module jar must be included in the web application.
Here a quick introduction to maven : http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
And here a basic config file for web apps : http://maven.apache.org/guides/mini/guide-webapp.html
You can create a maven project for your framework and another one for your web application. Then you just add your framework project as a dependency to your web application and it is automatically copied to lib path.
Generate a project for your framework:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
Generate a project for your web application:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
edit pom.xml of web application and add your framework as a dependency

Categories

Resources