Only deploy classes JAR from a Maven WAR Project - java

I have a Maven project which creates a WAR file as well as a JAR file (with the -classes classifier, using <attachClasses>true</attachClasses> in the maven-war-plugin config). When I run mvn deploy, both of these artifacts gets deployed to Maven Central (via Sonatype Nexus).
That's all very well, but it's not really necessary to deploy the WAR to Maven Central - just deploying the -classes JAR would be sufficient, since the WAR is always uploaded to our github site for download, and only the JAR will ever be used as a dependency to another project.
Is there a way to configure this in Maven? I thought that <primaryArtifact>false</primaryArtifact> in the maven-war-plugin config might fix it, but it did not.

According to the Maven WAR Plugin FAQs, this problem needs a bit of refactoring in order to be solved. You would have to split the project into one "classes" module that only produces the classes JAR, and a second module which builds the whole webapp into a WAR file. The WAR module can then include the JAR module as a dependency.
Different Maven profiles (one for building and deploying the JAR, another one to build the WAR and skip deployment) might be a solution too. But to be honest, that seems rather hacky and harder to maintain. I am not sure that it would make your life easier in the long run.

Related

How are dependency binaries included in my final built and installed .JAR in Maven?

1) I included a Spring Context dependency in my pom.xml project in Eclipse with Maven.
2) I ran the 'Install' phase on the project and it built properly, and the project was installed to my local .m2 repository
3) When I unzipped my .JAR, I only saw my single class that I created custom.
This brings up two questions:
1) Are external, dependency classes only included in your final built jars if a class from it is physically instantiated within your class?
and
2) How come, when I imported the SpringContextAnnotationConfig class into my class, and instantiated an instance of it, and installed my project, I STILL only saw my custom class when I unzipped my .JAR. Is this unusual? Or is the SpringContextAnnotationConfig now just written into the .class binary so that when I deploy my .jar to another JVM, it hass all its dependencies within my custom built .class binary?
Thanks
SOLUTION:
The problem was that I was expecting maven to do the same for a JAR output as it would for a WAR. When using the webapp archetype to generate a WAR file, Maven automatically packaged the required dependency jars into the WEB-INF directory.
I was basically trying to understand how a container on a remote, brand new server would run my classes without having the dependency binaries. I was attempting to run a maven built to produce a JAR file, which did not end up including my dependencies. However, when I ran maven install to build a WAR file, my dependencies were all there ready for deployment.
No, they are never included (unless you use a special plugin which does that).
See 1.
If you add this artifact as a dependency to some other project, its dependencies (and their dependencies, etc.) will be automatically added (this is controllable, so you can e.g. exclude them or change the version). But they are taken from pom.xml, not from the .jar itself. This also allows not to download same libraries a huge number of times.
For building fat jars, see e.g. How can I create an executable JAR with dependencies using Maven?. Specifically for Spring you may want Spring Boot.

Deploy gradle to directory

This may have been asked to death, but I can't find an answer. I have a pretty simple java project (not web), its built with ant with sources located in ./src and dependencies in ./lib. Looking to modernize it to either maven or gradle. Since I've had good results with gradle and android, decided to go with gradle. That means I'll be dropping ./lib for dependency management.
However, I cannot figure out how to use grade to deploy the project. I would like to deploy manually for now. So I would need to have jar build from the sources and having all dependencies copied into lib (or whatever) directory where jar is.
So far... I'm getting nowhere quickly.
Gradle Application plugin is perfectly suited for this use-case.
the task distZip will create a deployable zip file complete with dependencies.

Glassfish deployed project's dependency libraries

I'm new to Java and dependency management.
I have an EJB jar project with a few maven dependencies. When deploying the project to glassfish I get exceptions, that the classes from those dependencies are not found.
So I've added a maven plugin to copy over the dependencies from the local repository to {glassfish_dir}/glassfish/domains/domain1/lib every build.
I'm also using Netbeans.
Is copying over the the dependencies the proper way to go about this? Is there a better way to make dependencies work with glassfish?
The best way is to make the Maven pom.xml that builds the .war declare a dependency on your EJB jar project. That way your EJB jar and any libraries that it has a dependency on, will be included in the .war file that Maven builds.

How to build correct WAR file from switchyard project with Maven

The problems I face:
I need to build *.war with Maven from Switchyard project to deploy war, not jar as usual. And as I understand, *.war is also acceptable to make Switchyard work. But when I build war, no Switchyard services available though everything is deployed successfully on JBoss.
How to do that? Is there any hints how to correctly assemble war for Switchyard?
So, I found the answer.
Unfortunately there is no instruction how to build correct WAR for Switchyard project, so I hope my experience will help some day to some people.
All that you need is to move both switchyard.xml and beans.xml from src/main/META-INF to src/main/webapp/WEB-INF and build WAR after that ( don't forget to add war to the pom.xml ).
That solved the problem, now Switchyard services are available (but not work).

Where should I add Java files in web (WAR) project in Maven?

Where should I have my Java source folder in the Maven web application architecture which results in a WAR?
Suggestions needed.
Maven web applications typically do not include java source code. The maven approach is to create a maven project for the "logic" of your web application (this will build into a jar) and create a second maven project for the webapp portion of your web application (this will build into a war). Then in the webapp portion, you introduce a dependency on the "logic" portion.
The end result is that when you build / test your logic jar (which contains servlets, etc), you will deploy that to your local repository and then build your war (which contains jsp pages, web.xml configs, etc).
The basic structure that is standard for Maven project is the following.
src/main/java Application/Library sources
src/main/resources Application/Library resource
src/main/filters Resource filter files
src/main/assembly Assembly descriptors
src/main/config Configuration files
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site
Following the Maven recommendations and normal behavior makes it easier for other people familiar with Maven to easy recognize and understand the structure.
Source/Read more
Unless you explicitly specify it differently in your pom.xml (productive) Java source files in a Maven project go to src/main/java.
Java files always go in src/main/java and its advised to keep it that way.

Categories

Resources