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

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!

Related

Maven on the empty project downloads tons of dependencies

I use Maven on daily basis in my work for more then 5 years. But I never tried to test the minimum dependencies project.
So I created a new directory on my disk and put inside a pom.xml file. It is the most simple pom file you can create. It contains only this:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>info.jikra</groupId>
<artifactId>whatever</artifactId>
<version>0.1</version>
</project>
And that's it. There is nothing more. No other directories, not a single Java file, nothing.
Then I cleared my local repository and ran mvn clean install in the folder with my pom file.
Maven downloaded tons of dependencies I don't need. My project is empty, there is only one pom file. Yet, there are more then 7,6M of files in my local repository, now.
I'm not any kind of Maven master, so I wonder why all those dependencies are necessary. Does anyone know?
Your project has some predefined plugins declared as well as packaging (default is jar) which defines a list of additional plugins as well as their bindings. Those are getting downloaded along with their transitive dependencies.
You can run mvn help:effective-pom in order to see what's actually present in your project.
You could also see plugins and their dependencies with: mvn dependency:resolve-plugins

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 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.

Spring maven dependencies not copied in the WEB-INF/lib folder

I have a working GWT-App Engine web project which works before I added the Spring dependency in the POM. Now when I run the application, this error is thrown (and ultimately App engine won't load):
java.lang.ClassNotFoundException:
org.springframework.web.context.ContextLoaderListener
I think the problem is that the Spring jars are not copied to the WEB-INF/lib folders as I can't see those in that folder.
Any ideas how Spring jars will be copied to that folder?
Update:
Here's the complete POM.xml
Add <packaging>war</packaging>. Currently Maven doesn't copy any dependencies artifacts into WEB-INF/lib because that's a default behaviour of jar packaging you used so far for this artifact.
I think you should add
<packaging>war</packaging>
after
<groupId>mygwtapp</groupId>
<artifactId>mygwtapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
Add them as Maven dependencies to your war and make sure their scope is not "provided".

Copying a jar from package to a resource folder of a web project using 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>

Resources