5

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.

tjholmes66
  • 1,850
  • 4
  • 37
  • 73
  • 1
    I am a bit confuse, what do you mean by : "appears as a directory, and not a jar." If you extract WEB-INF/lib/myproject-dao-0.0.1-SNAPSHOT.jar from the war, it is a directory or a jar file? – drgn Aug 15 '13 at 16:44
  • By default, the dependencies (e.g. JARs) are packed in the WAR into `WEB-INF/lib`, what did you expect or what is bothering you? – LaurentG Aug 15 '13 at 16:46
  • Can anybody explain what is the problem ?? – saurav Aug 15 '13 at 17:00
  • Yes ... I know a WAR is essentially a zipped up directory. If you're on a windows system like I am and you look at WEB-INF/lib, then spring-framework-core-3.2.3-RELEASE has an icon that shows it is a .jar file. When I look at the icon for my jar it's the icon for a folder/directory. Put it this way ... if you were on windows and created a directory called test.jar ... windows knows it's really a directory, and not a file. This is even evident in Eclipse under Maven dependencies, that my jar icon looks like the icon for a directory and not a file. – tjholmes66 Aug 15 '13 at 18:35
  • if you think this isn't a problem, you're wrong ... when I deploy and try to run my web war file ... my web-app cannot find any of the classes from the dao jar. So, essentially maven is creating a directory with the ".jar" in it's name, it is clearly not packing the file as a jar. Also, think about it this way ... if you click on a jar, you need a tool to look into the file to see the contents, if you click on a folder "test.jar" which is a directory, it just opens up. – tjholmes66 Aug 15 '13 at 18:37
  • 1
    Actually you don't need a tool a jar file is basically an archive. see here http://en.wikipedia.org/wiki/JAR_%28file_format%29. Are you using the maven-assembly plugin? (http://maven.apache.org/plugins/maven-assembly-plugin/) or the war pluging? (http://maven.apache.org/plugins/maven-war-plugin/) Also could you provided your whole pom.xml. I am wiling to help but i need more information – drgn Aug 15 '13 at 19:21
  • So i figure that you were building with m2e and not directly maven in command line. I think there is configuration for the m2e that allow you use directly another project within the same workspace thus using the folder instead of the jar.(I'm really not sure of that) Also could you answer your own question, then accept it so it get tag as resolve?. Have a nice day – drgn Aug 15 '13 at 20:04

1 Answers1

6

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!

tjholmes66
  • 1,850
  • 4
  • 37
  • 73