maven how to include /src/main/resources - maven

I have a mavenized project with jar packaging, have standard layout:
/src/main/java
/src/main/resources
When I execute: mvn package
I am expecting the resulting jar (non-executing) file to include the resources folder and everything inside that folder from the project(i.e. some xml files), but when I open the
jar file I only see the com folder where and the contents of the resources folder in the top level...
I think the above is default for maven3, but I want to include the resources folder in the jar file...

You will not see src/main/resources in resulted jar file.
You will only see the contents of src/main/resources in the jar file.
So, if you did not create a package structure for your resource files and dumped everything into src/main/resources directory, you will see your resource files at the top level of your jar.
If you want your resources appear under certain directory in the resulting jar file, create such a directory under src/main/resources tree.

Related

Spring: How to access a txt file located outside the jar?

Currently, I am passing the environment specfic configs in a properties file outside the jar using -Dspring.config.location=/apps/conf/application-dev.properties. I also have properties file included in the jar named application.properties which has configs that don't change often. There's one config called nameFile:
nameFile=classpath:config/nameList.txt
And the actual nameList.txt is included in the jar under spring-app/src/main/resources/config/nameList.txt but I want to move the nameList.txt outside the jar.
What I've tried:
Changing it to nameFile =${auth.nameList} and added this to /apps/conf/application-dev.properties like:
auth.nameList=classpath:/apps/conf/nameList.txt
And I moved the nameList.txt file from the jar to /apps/conf/ location but the Spring application fails because it cannot find the txt file. How do I fix this? I want to move the nameList.txt to outside the jar.
Removed classpath:
auth.nameList=/apps/conf/nameList.txt
No luck!
When you provide a path, classpath will "tell" the JVM that the location is within the jar/war file. If you have the resource you need in an external location, you will need file instead.
auth.nameList=file:/apps/conf/nameList.txt

Maven Build - copy resource from a directory to another directory

I have a project structure like:
Web Pages
|
|----WEB-INF
|----js
|----css
|----dist
|----other files
I want to remove all the files and folders except the WEB-INF folder and also copy the contents from dist folder directly into the Web Pages and then remove the dist folder as well during the build process.
I have tried maven-war-plugin but I am not able to do it.

Maven. How to create .jar with local .txt files

I use Intellij Idea.
Java-classes use .txt files which are placed in folder where .java files are situated.
I created .jar file with maven and pom.xml. But there are no .txt in .jar file.
How to add them there?
UPDATED:
I have such structure src/main/java/Project-name/ for .java files. And src/main/resources/for .txt files. Within .java files I access files as ../../resources/filename.xml and all works fine. But .jar-files created with maven says:
Unable to open "../../resources/filename.xml"
How correctly should I declare filenames?
You are going against the convention. Java source goes to src/main/java but your resources must go to src/main/resources. The rest ist handled by Maven.

ClassNotFoundException on Tomcat Server

I am using this tutorial to set up Tomcat Server. After I have put the HelloServlet.java in classes and Web.xml in the WEB-INF folder and I'm giving the command
localhost:9999/hello/sayhello
On the browser. I'm always getting ClassNotFoundException. If anyone can tell me where am I going wrong.
I'm using JDK1.6.0_30, and Tomcat7 for my sample application.
You need to put the compiled HelloServlet.class file (not the .java file) in the WEB-INF/classes folder.
Compile HelloServlet.java and place the output class file HelloServlet.class into
<TOMCAT_HOME>\webapps\hello\WEB-INF\classes\HelloServlet.class
One thing you should check is that, the HelloServlet.class should be in a package. Looks like you have a long way to go with Servlet and JSP... I recommend Head First Servlet and JSP for your reference.
In your 'WEB-INF' of 'classes' folder place the .class files and if your using JDBC, jsp's just Copy the .jar executable files into 'lib' folder. and make sure that xml file should contain the proper information.
you should follow below Web Application Directory Structure
WEB-INF/ --
web.xml --xml file
classes/ ---classes folder here we keep .class files Myservlet.class
lib/ ---lib folder here we keep all .jar files. Myapp.jar
Welcome.html
Welcome.jsp

M2E Removes My Source Directory?

I have an existing library that I am building in Eclipse and have added the Maven nature to my project using m2e to add dependencies. When I convert it to a Maven project, my existing source directory (and my bin) become normal folders. Is there a reason for this? I am new to Maven, so I am likely doing something wrong, just not sure what...
My project structure is as follows:
workspace
project
src (in build path)
resources (in build path)
bin (output dir)
I tried both "mvn eclipse:eclipse" and right click on project -> Configure -> Convert to Maven Project, and both removed my src and resources folders from my build path, and after changing the structure to the below, changed the output to target/test-cases. Even if I manually adjust the build path and output, my dependencies don't resolve.
workspace
project
src (no longer in build path)
resources (no longer in build path)
bin (no longer output)
target (new output dir)
test-cases (empty)
I think you have the following structure when working with Eclipse (without Maven):
/workspace
/project1
.project
/src
/bin
But Maven want to use the following structure
/workspace
/project1
.project
pom.xml
/src
/main
/java
/resources
/test
/java
/target
/classes
/test-classes
and so on. So it is normal, that the folder src is no more directly a source folder for Eclipse, but now there are src/main/java, src/main/resources, ...
So it would be easier in the beginning to start with a new Maven project, and move your original sources to the directories they should belong to. Maven has a long tradition with its "convention over configuration", to deviate from that is possible. Have a look at the answer to "Handling unconventional source directory ..." to fix this.

Resources