Eclipse misplaces maven resources in output jar - java

I have a maven project loaded in eclipse that when I go to export it as jar, the src/main/resources files always end up in the /resources directory inside the jar. I found a kludge to get around this by putting all my resources in the root directory of the project, but it's getting very messy very fast. Is there any way to configure it so the resources output to root of jar?

you dont export a maven project as jar.
you clean package it and you get the resulting jar at the target folder.
right click the project's pom.xml and run it as maven build.

Related

In maven pom.xml, how to specify that I want to copy a ".xml" file into my target folder?

I'm building a mybatis project, under my src/main/java I have folder structure like this:
java
--mypackage
--beans
UserBean.java
--mapper
UserMapper.java
UserMapper.xml
--service
UserMapperServer.java
--resources
when I run "mvn compile", the java files will be compiled into "target/classes" folder, but my "UserMapper.xml" file is not automatically copied into "target/classes/mypackage/mapper" folder, so the program will fail to start/debug.
If I move the file into "resources" folder, then "mvn compile" copies it into "target/classes" folder, but not "target/classes/mypackage/mapper" folder, still not able to run/start.
So my question is, in "pom.xml" how to specify my required "copy" file instruction?
Thanks a lot.
Maven convention suggests putting the sources into src/main/resources folder
In this case maven will pick them an put to jar automatically, no need to use src/main/java for this.
Now, resources folder can also have inner folders
So, your layout should be (to comply with maven conventions):
java
--mypackage
--beans
UserBean.java
--mapper
UserMapper.java
--service
UserMapperServer.java
--resources
resources
--mypackage
--mapper
UserMapper.xml

Deployment Assembly configuration not working with mvn package

Below is the maven project structure I have in eclipse:
And the deployment assembly configuration is as shown below:
If I package the war with maven command (mvn clean package) and deploy the war on tomcat, the "resource" folder under WEB-INF is not getting created as I mentioned in deployment assembly configuration.
The server side folder structure created after deployment is as in the image below:
But, if I export the war from eclipse "Right click on project -> Export -> War file" and deploy in tomcat, it is creating the "resources" folder.
Kindly help me with this.
Thanks in advance!!!
If you look at the documentation for the maven-war-plugin here:
maven-war-plugin
You'll see that by default the contents of the "resources" folder gets placed inside the "classes" folder inside the WAR, so for a maven build you should find your resources in there.
Your Eclipse configuration is different, placing the resources in a separate "resources" folder under WEB-INF. Exporting from Eclipse will use this configuration rather than the maven configuration.
I would recommend changing your Eclipse configuration to match the maven one by changing the Deploy Path to "WEB-INF/classes"

Clean all files except target in maven

In maven is it possible to clean all files inside project directory except target directory after maven compile step? How can I setup this?
My requirement is something like this -
I run maven compile command which generates classes file under target directory.
Now I want to copy only generated target folder and pom.xml to another directory and build jar/war from that location, can I do something like this?
Also can maven copy a external properties file(outside of project) to projects WEB-INF/classes folder?

eclipse, wtp and tomcat configuration for source folder

I have a java project in eclipse which is having jar file reference of other projects.
Now, I have removed those jar files and added projects in eclipse, no compilation error in eclipse. I have configured Tomcat server in eclipse.
But server doesn't get loaded when i add project instead of jar files.
I have observed following path in eclipse workspace:
<<Eclipse IDE workspace>>\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ESW\WEB-INF
under WEB-INF folder, jar files and class files are located. All jar files are loaded to 'jar' but under classes folder no class files are present at this directory. here i am expecting class files of the project which i have added in source folder instead of jar files. how can i do it?
Please try to use the Deployment Assembly option (Project Properties ---> Deployment Assembly ) to configure which files to be deployed to the WTP Tomcat . Before eclipse 3.5 or before , this option is called something likes J2EE Module Dependencies
Deploy path is relative to the your <<Eclipse IDE workspace>>\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ESW\ while source path is relative to your project folder.

Jars not copied to Tomcat's lib folder

I am doing some basic spring stuff and stuck at some point.
I am getting ClassNotFoundException whenever I deploy my application on Tomcat.
I observed that the jars are not copied to Tomcats lib folder.
When I copied the jars manually to Tomcats lib folder it works fine.
Please let me know if I am making any blunder.
PS - I am using Spring tool suite 2.6.0,Tomcat 6 and its a Maven project.
Thanks.
Go to "Project properties -> Deployment Assembly page".
This page describes how your application will be packaged for deployment or export. And added new source "Maven dependency".
From Deployment Assembly page, Click Add... button
Then select "Java Build Path Entries"
"Maven dependency" should be in the list
It solves the issue and all jar got copied to tomcat
If you define "war" packaging for your maven project then your dependent libraries should be automatically copied to the WEB-INF/lib directory of the created .war file by the Maven WAR plugin.
Put your jars in the WEB-INF/lib directory:
In Project properties -> Deployment Assembly you should have a Source of /web with a Deployment Path of /. If this is there then any jars in the web/WEB-INF/lib directory will get deployed and picked up by the class loader. Note that sub-directories will not be picked up though, so put your jars directly in the lib directory.
You run three command in cmd or bash(where your pom.xml file placed) to get jar file and copy it into your WEB-INF/lib folder
mvn compile
mvn package
mvn install
I find only this way for resolved my problem:
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

Categories

Resources