Copying a jar from package to a resource folder of a web project using Maven - maven

I have 2 java projects which are Maven projects.
One is a desktop app which has a plugin (maven-assembly-plugin) that I am using to 'package' all the dependencies that this application requires into one large jar. When this project is packaged (i.e. mvn clean package), it creates two jar files in the target folder. One of the jar file (childApp-0.0.1-SNAPSHOT.jar) is a smaller which doesnt have the dependencies embeded and the other jar file (childApp-0.0.1-SNAPSHOT-jar-with-dependencies.jar) is a larger file that has all the dependencies in it.
My other project is a web-based project and it creates a war file after it is packaged. I require the (childApp-0.0.1-SNAPSHOT-jar-with-dependencies.jar) file to copied to the resource folder of the web based project. More specifically, the jar file needs to be copied to the {webApp}\WEB-INF\classes folder.
How can I achieve this using Maven?
I have tried putting these two projects under a parent multi module project where I have the childApp packaged first and having a the webApp have a dependency on this childApp. This kind of works except it is copying the (childApp-0.0.1-SNAPSHOT.jar) file into the {webApp}\WEB-INF\lib folder. I can handle the file location being different from what I ideally wanted but I cant seem to get Maven to include the other large (childApp-0.0.1-SNAPSHOT-jar-with-dependencies.jar) file into {webApp}\WEB-INF\lib folder.
Any thoughts would be much appreciated.
ps: my web-app project is essentially a JNLP project.

You could try adding an additional dependency for the larger jar like the following:
<dependencies>
<dependency>
<groupId>some.group</groupId>
<artifactId>childApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<classifier>jar-with-dependencies</type>
</dependency>
...
</dependencies>

Related

include jar of one module to another

I have maven projet with this architecture:
++parent-project
+module-a
+module-b
module-b is a web application. it will be run on Jboss AS 7.1.1. I'm using netbeans IDE.
Now module-b depend on module-a. this is a porm section of module-b:
<dependency>
<groupId>groupid</groupId>
<artifactId>module-a</artifactId>
<scope>compile</scope>
</dependency>
When i build the war file of module-b, module-a is not present to lib folder ( in war file. i open it with archive explorer ). therefore JBoss return ClassNotFoundException.
I'm tried differends scope ( compile , provided , runtime , test ). But nothing.
Please how can i solve this.
First of all, I think you should try to see how does it work in "pure" maven, without the IDE at all (NetBeans). So my answer will be based only on maven knowledge:
A couple of facts:
Module b has to have the following in pom: <packaging>war</packaging> This will instruct maven that you really want to get a war from this module.
When packaging WAR is specified in some pom, maven will take all the dependencies defined in this pom and will put them into the WEB-INF/lib folder of the war. Automatically. Of course, you can customize the output, but its more advanced stuff (see Maven WAR plugin if required)
All the dependencies have to be defined with group id, artifact id, and version at least. So make sure that you have the dependency on module a with version. There is no need to fiddle with scopes in this case. The default scope (if you don't specify a scope at all) is 'compile' which is fine.
Go to the directory of module b and from within the directory type: mvn dependency:tree. Once its done, please carefully observe the output, especially make sure that module a is listed (with a correct version) in a tree.
Sometime to make sure that no stale artifacts reside in the local m2 repository you might want to delete all the jars of your project from there and then execute the mvn package command again. The war has to be created in module b/target - and this is the WAR you should check out.
Note, all these steps are done without any interaction with NetBeans at all.

How do I build an exploded WAR in a multi-module Maven project and also include sibling JAR files in its WEB-INF/lib directory?

I’m using Maven 3.3.3 with the Maven WAR 2.6 plugin. I have a multi module project with both JAR and WAR projects within it. My WAR projects depend on my JAR projects, however, I do not want to package my WAR files, opting instead to keep them in an exploded form. So I tried running this command
/usr/local/apache-maven-3.3.3/bin/mvn -B -f /home/jboss/.jenkins/jobs/subco/workspace/pom.xml -B clean prepare-package war:exploded -DskipTests -T 3
Although this keeps my WAR exploded, the JAR projects do not get included in my web application’s target/myproject/WEB-INF/lib directory. Instead what is included are empty directories where my JAR files would have been, for instance
.jenkins/jobs/subco/workspace/myproject/target/myproject/WEB-INF/lib/child-jar-module-2.0.0-SNAPSHOT.jar/
How can I keep an exploded WAR, include my JAR files, and do it all from the command line (as opposed to creating a custom profile in my parent pom to do it for me)?
Edit: If you think this is a duplicate of another one, notice the part of my question where i point out that empty directories are generated when using the "prepare-package" phase. I don't want that.

How to avoid creating empty jar in non-Java Maven project?

I have a Maven project which uses a jdeb plugin to generate a deb (Ubuntu installer) package with some simple shell scripts that I would like to send to a customer to be able to deploy easily. This project has no Java whatsoever. I am not specifying in the pom.xml that it should package a jar. However, an empty jar does get packaged in the project's target directory anyway.
How can I avoid creating this empty jar file? I am not sure if it gets included in the deb package but if it does, it is just dead weight size-wise.
Got it figured. In the pom.xml the following needs to be added at the same hierarchy level where the <name>...</name> is for that project:
<packaging>pom</packaging>
Otherwise
<packaging>jar</packaging>
is assumed as a default.

Java Maven Project appears in another project as directory not a jar

So, I created a Spring Maven Project, a Dao Project, which works great. All the code is in the correct place, all the unit tests run, and I can do a mvn clean install. I can see in the target directory that there is a build jar, and everything in it looks fine. I can also confirm that when I check my local .m2/repository, the latest jar I just built is in there.
Here is a small segment of that pom.xml:
<modelVersion>4.0.0</modelVersion>
<groupId>com.tom.myproject</groupId>
<artifactId>myproject-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>My Project DAO</name>
Now, I am creating a new Spring Maven Web-Project for my UI, and this pom.xml starts out like:
<modelVersion>4.0.0</modelVersion>
<version>0.0.1-SNAPSHOT</version>
<groupId>com.tom.myproject</groupId>
<artifactId>myproject-ws</artifactId>
<name>My Web Project</name>
<packaging>war</packaging>
Also in this pom.xml file, one of the first dependencies I have is:
<!-- My DAO ProjectDependencies -->
<dependency>
<groupId>com.tom.myproject</groupId>
<artifactId>myproject-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
I can see in eclipse that when I try to access the classes from this dao jar, that seems to work fine. When I compile the code here, that all works fine. And when I do a "mvn clean install" to build this war, I can see the target directory, and all the needed classes and jars come in, obviously there are hibernate, logging, and spring classes that are there in the WEB-INF/lib directory.
Now this is he part where the question comes in ... Why does the myproject-dao show up under WEB-INF/lib as WEB-INF/lib/myproject-dao-0.0.1-SNAPSHOT.jar but appears as a directory, and not a jar?
Certainly when I pull in other jars, they are just .jar files and not directories.
I should add, when you look in the Eclipse project under maven dependencies, the icon for all the other jars show them as files. The icon for my myproject-dao.jar uses the icon for a folder/directory, so clearly a directory named "myproject-dao.jar" is being created and not a file with zipped up contents into a jar file.
When I build the war file, and deploy the war, the app says it cannot find any of the classes in the myproject-dao.jar directory. I have to delete the FOLDER "myproject-dao.jar" from the WEB-INF/lib directory and manually copy over the FILE "myproject-dao.jar". I can then clearly see the icon that means this really is a file.
This is probably a simple fix, so if you can help me out, that would be great.
ANSWER:
I wasn't using the maven-assembly-plugin. But, I did find out what the problem is.
Both my DAO project and the WEB project are in the same workspace, and the DAO project was open, as a result, the maven dependency shown in Eclipse was a folder icon, and not the jar icon. When I closed the DAO project and looked back at the maven dependencies on the WEB project pom.xml file ... NOW it shows the icon as a jar and not a folder.
So, all I have to do build my dao.jar file with maven, and when it is successful, it is done.
Then I have to CLOSE the project within Eclipse.
In my web project pom.xml file, the icon will now show this as a jar file, and when it builds, it will pull in the jars.
I imagine this is a benefit if one is working on several prior project, you really do have the chance to look at what is in the jar file while the war is running.
In my case, I know my dao.jar is working, so I can build it, and close the project, and then make use of that jar anywhere I need it.
I wasn't using the maven-assembly-plugin. But, I did find out what the problem is.
Both my DAO project and the WEB project are in the same workspace, and the DAO project was open, as a result, the maven dependency shown in Eclipse was a folder icon, and not the jar icon. When I closed the DAO project and looked back at the maven dependencies on the WEB project pom.xml file ... NOW it shows the icon as a jar and not a folder.
So, all I have to do build my dao.jar file with maven, and when it is successful, it is done.
Then I have to CLOSE the project within Eclipse.
In my web project pom.xml file, the icon will now show this as a jar file, and when it builds, it will pull in the jars.
I imagine this is a benefit if one is working on several prior project, you really do have the chance to look at what is in the jar file while the war is running.
In my case, I know my dao.jar is working, so I can build it, and close the project, and then make use of that jar anywhere I need it.
Thanks for the help!

Maven update contents of an EAR file

I have an EAR file which I want to update. I do this in Ant by unzipping the EAR and then the JAR, replacing a few files and then repackaging it.
I am trying the same with Maven but with little success and it is also confusing.
So far I have done
1) installing the EAR file in the local maven repository
2) unpack it
3) Replace the file I need
Now I am not sure how to get back the new EAR file. Everything is in the repository.
From my understanding, the EAR plugin packs everything in the ejb, war folders and spits out the ear. But since I directly got the EAR file, I do not have any project per se.
Any suggestions on this?
Also is there a good tutorial on Maven?
Thanks,
You should probably perform the following steps, using a "blank" Maven project and assuming the EAR file is already inside a repository (pushed by another project/tool):
use the dependency:unpack mojo to get and unpack (inside the /target/ folder of your project) the EAR
modify the contents
use the assembly plugin to repack that EAR file
Then do what you need to do with that new EAR file.
A "blank" Maven project would mean that it will not act as a usual project (compile sources, package output...), but rather serve to manipulate existing artifacts through specific plugins.

Resources