Spring boot generated jar file just work inside root of project - maven

I am new in Spring boot and I think this issue is very basic.
I created an application using Spring Boot and everything is good in dev environment. But when I copy the jar file from target directory to another machine and run "java -jar" it doesn't render jsp pages with following error:
There was an unexpected error (type=Not Found, status=404)
when I copy src folder from project root to same location it works fine.
It seems the jar file just work from project's root.
Here is my configurations:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Section -->
<!-- Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
and my project structure:
and I run following at command line:
mvn clean package
I have following in my target directory:
I can run the application using
java -jar target/springboot-in-10-steps-0.0.1.jar
Application works as expected and it renders welcome page.
When I go to target directory and run same command:
java -jar springboot-in-10-steps-0.0.1.jar
Application gets launch but it doesn't render the welcome page.
When I copy src to target everything is fine and I can see welcome page. It seems Spring boot doesn't find WEB-INF/jsp directory in the jar file.
Did I miss something in spring boot configuration or application.properties?

I could fix it by changing packaging from jar to war and interestingly this works:
java -jar springboot-in-10-steps-0.0.1.war
I think there are ways to change the structure of files and folders within generated jar file using repackage features.

Related

Why does servletContext.getResourceAsStream get files outside of the executable jar?

I'm in the process of switching from war-packaging of my Spring Boot app to using an executable jar.
My resources are under src/main/webapp and, according to Spring Boot docs (if I understand correctly), should not even be included in the final executable jar:
Do not use the src/main/webapp directory if your application is packaged as a jar. Although this directory is a common standard, it works only with war packaging, and it is silently ignored by most build tools if you generate a jar.
This is confirmed by the fact that in the target jar I cannot find these resources at all. Unfortunately the executable jar structure description seems to say nothing about where the resources are placed.
However, to my surprise, calling servletContext.getResourceAsStream("/myresource.js") in the application DOES find the resource correctly. And the resource seems to be served from my src/main/webapp directory (even when running the jar in standalone manner e.g. via java -jar myjar.jar.
Also:
calling servletContext.getResourcePaths lists the contents of my src/main/webapp directory
calling servletContext.getRealPath("/") returns the exact path on the filesystem to my source files e.g. c:\path\to\my\project\src\main\webapp.
Why is this happening? I would expect only what's inside my jar to be available to the application. Does the embedded tomcat place my source files in the context root for some reason? Any links to relevant documentation would be appreciated.
P.S. In my pom.xml you will see standard things like:
<packaging>jar</packaging>
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
...
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
...

Add external library .jar to Spring boot .jar internal /lib

I have an external .jar that cannot be imported from public repositories using pom.xml, it's sqljdbc41.jar.
I can run the project locally from my IDE, and everything will work. I referenced the library after downloading it like so:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc41.jar</systemPath>
</dependency>
When I run mvn clean package to create my .jar file and try to run the created .jar, a mistake will pop up, which mentions the SQL Server references are not valid. I then extracted my .jar file and true enough, everything that is referenced in the pom.xml file properly gets downloaded and added, however, my SQL Server does not.
I can, in a very hacky way* just manually add the sqljdbc41.jar to my /lib folder after it's been compiled as a .jar, and it'll work, however that seems highly unoptimal. What would be a better approach?
*Opening the .jar file with Winrar, going to the /lib folder, manually selecting my sqljdbc41.jar file, then make sure to select the No Compression option bottom left where Winrar gives you compression options, in case you find this by Google and no one answered.
you can set 'includeSystemScope' to true.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
You could install the sqljdbc41.jar in your local repository :
mvn install:install-file -Dfile=path/to/sqljdbc41.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc41 -Dversion=4.1 -Dpackaging=jar
And then declare the dependency as a standard dependency :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
</dependency>
If you use a remote artifact repository (nexus, archiva...) you also need to deploy the artifact on this repository. You can find more here : https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
Another way, you can put it into the resources folder, such as resources/lib/xxx.jar, then config the pom.xml like this:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/sqljdbc41.jar</systemPath>
</dependency>
In Spring Boot: I also faced similar issue and below code helped me.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
It works for me:
project {root folder}/libs/ojdbc-11.2.0.3.jar
pom.xml:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>11.2.0.3</version>
<scope>system</scope>
<systemPath>${basedir}/libs/ojdbc-11.2.0.3.jar</systemPath>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
In my case, the fault was providing a version number without "dot" in tag:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
This one works:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1.8</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
When Spring-Boot projects are used with maven or gradle plugins they packaged the applicaiton by default as executable jars.
These executable jars cannot be used as dependency in any another Spring-Boot project because the executable jar add classes in BOOT-INF/classes folder. This means that they cannot be found when the executable jar is used as a dependency because the dependency jar will also have the same class path structure as shown below.
If we want to use project-A as a maven dependency in project-B then we must have two artifacts. To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency.
To configure a classifier of exec in Maven, you can use the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
So the MAJIC WORD here is <classifier>exec</classifier> this will create a jar structure as below and then it could easily be conusmed by spring-boot project as maven dependency jar on class path.
The above plugin need to be add in project-A pom that is going to be used as dependency in project-B. Same is explained in spring documentation section 16.5. as well.
In order to work through the local repository, the target .jar file that we will work with must be in the s2 folder. Several methods can be used for this:
The file can be taken manually and put in the relevant place (not
preferred). The same process can be done by installing it via the
console.
Relevant Remote URL is written in the .pom file dependencies and
automatically places it in the s2 folder when Intellij is refreshed
(validate) in the IDE used.
The same process can be done by addressing the .pom file dependencies via the centeral repository.
Attention: ComponentScan should not be forgotten for the related jar work on SpringBot.

Maven Not Compiling the java files

This is My Folder structure -
![Project explorer][1]
--project>
--src
--main
--java
--resource
--target
pom.xml
This is My Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong</groupId>
<artifactId>spring-security-loginform-xml</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringSecurity Custom Login Form XML</name>
<url>http://www.mkyong.com/tutorials/spring-security-tutorials/</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>mkyong</id>
<name>Yong Mook Kim</name>
<email>mkyong2002#gmail.com</email>
</developer>
</developers>
<properties>
<jdk.version>1.6</jdk.version>
<spring.version>3.2.8.RELEASE</spring.version>
<spring.security.version>3.2.3.RELEASE</spring.security.version>
<jstl.version>1.2</jstl.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<!-- jstl for jsp page -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
</dependencies>
<build>
<finalName>SpringSecurityHelloWorld</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
</plugins>
<sourceDirectory>${basedir}/src</sourceDirectory>
</build>
</project>
While i am going to modify the code in java file or the controller those are not effecting while I am running the project. I think Maven is not compiling the Java code.
Is the folder structure is correct ?
Where the class files are generated in the project ?
Can any one suggest with explanation.
Based on your question and the comments below it I try to give you a answer:
is the folder structure correct?
Yes ist is. It follows the Maven standard directory structure described here: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
It however misses a webapp-folder (see next point) which will not make your build fail but end in a war not containing a WEB-INF folder - hence it is not a standard webbapplication as defined by the Java EE standard (http://docs.oracle.com/javaee/6/tutorial/doc/bnadx.html).
Where are the class files generated in the project?
After maven runned the phase compile (mvn compile) of the default lifecycle (http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html) the compiled class files will be placed under /target/classes.
If you continue to the phase package (mvn package) maven will build a war (jar is the default packaging type which was overwritten in your pom) containing your classes as well as your resources which is placed under /target
Note: If you are building a webapplication you place your website's content such as a deployment descriptor (web.xml) or HTML/JSP/JSF pages etc. under a folder src/main/webapp (the WEB-INF folder should be under src/main/webapp/WEB-INF).
Regarding the comments disscussion
If you expect to see changes in Java files on the fly Maven will not support you out of the box. Maven is a build tool - e.g. if you change something in your project you will request maven to build the project again to have the changes ready in a deployable form (e.g. you have a war file in your /target folder which you can then deploy).
If you want to see changes on the fly you should try a websearch for topics such as Hotdeployment, JRebel, embedded jetty since there are quite a few options available to archive this.
One general approach to have hotdeployment "out of the box" is to point the exploded directory of a hotdeploy-supported (web)(application) server to the exploded directory of your maven build. Right on the same level as you will find your spring-login-security-xml.war the is a folder spring-login-security-xml which contains the unpacked webapplication. This will however not spare you to have a build per change.
This is the first issue that popped up for me when my class files were not being generated. My fix does not apply to this question, but I'm mentioning it here in case it helps someone else.
I had a quick copy and paste setup with a single top level pom.xml. In it I had set
<packaging>pom</packaging>
which caused the java files to be ignored. The fix was to remove the packaging tag, letting it default to jar.

How to deploy in a gwt-maven project

first of all, I'm not sure which folders and files i have to deploy in a gwt-maven project
I've got:
.gwt
.settings
bin
src/main/java
target
war
pom.xml
I'm pretty sure, I've to deploy the pom.xml somehow and the target folder. But my target folder doesn't contain a pom.xml which I need for deploying on a jetty server
Second:
I've installed maven on my webserver, but apart from embedding the jetty-maven-plugin in the pom.xml (by
org.eclipse.jetty
jetty-maven-plugin
)
I have absolutely no clue how to get this project running on a jetty server.
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
SiedlerVonCatanC
SiedlerVonCatanC
war
src/main/java
src/main/java
*/.java
maven-compiler-plugin
3.1
1.7
1.7
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test</goal>
</goals>
<configuration>
<module>main.java.de.swp.catan.SiedlerVonCatanC</module>
<runTarget>SiedlerVonCatanC.html</runTarget>
<hostedWebapp>${webappDirectory}</hostedWebapp>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<!-- GWT -->
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-servlet</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<version>2.5.1</version>
</dependency>
<!-- SmartGWT -->
<dependency>
<groupId>com.smartgwt</groupId>
<artifactId>smartgwt</artifactId>
<version>3.0</version>
</dependency>
<!-- Event Service -->
<dependency>
<groupId>de.novanic.gwteventservice</groupId>
<artifactId>gwteventservice</artifactId>
<version>1.2.0</version>
</dependency>
<!-- Java-Mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
<!-- htmlunit (wird im Projekt irgendwo genutzt) -->
<!-- <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId>
<version>2.4</version> </dependency> -->
<!-- Guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<!-- Connector for JDBC -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>com.smartgwt</id>
<url>http://www.smartclient.com/maven2</url>
</repository>
</repositories>
<version>0.2</version>
have the maven-jetty-plugin included in your pom , under plugins and then you can run it as
mvn jetty:run
thanks
If you want to use maven plugin to run embedded jetty for development then easy way using command-line (which I recommend for start) is:
Edit: Please note this is not related to the existing project.Its process from scratch. Just run these command from a new directory where you would like a new gwt project to be created.
Dowload gwt maven plugin http://mojo.codehaus.org/gwt-maven-plugin/. In command line list you can see it as org.codehaus.mojo:gwt-maven-plugin (Maven plugin for the Google Web Toolkit.) I see that you have this plugin in POM, but if you are at loss here the simple steps from command-line to achive this are:
mvn archetype:generate
enter org.codehaus.mojo:gwt-maven-plugin in search prompt
comfirm the resutl with enter
set-up the maven project configuration as prompted
run the project with mvn gwt:run from the folder where you have pom.xml
These two steps will run a sample project for you which you can further modify and experiment while learning how it works.,
As for directories:
The maven compiles all your stuff in target directory and that is used for deployment or running embedded jetty.

Why does maven not copy the properties files during the build process?

Nothing I've found has been able to help me solve this one specific case. I recently switched from a plain old java web app project (which was working) to a maven web project. I get the following runtime exception:
java.util.MissingResourceException: Can't find bundle for base name com.myapp.config, locale en
I am using Netbeans to create a JSF 2.0, Spring, and Hibernate web app. I have the following directory structure:
src\main\java\com\myapp Contains config.properties
src\main\resources Empty
target\myapp\WEB-INF\classes\com\myapp Contains compiled class files without config.properties
src\main\java\com\myapp Contains config.properties
Inspection of the WAR file in the target folder does not show any sign of the properties file so it's as if the Maven build plug-in is not copying over properties files. I know there is a tag you can place inside the pom but it didn't work for me. The link below mentions that the resources folder (empty for me) has its contents included during the build but if that is the case, how do you do it from Netbeans? I just want the properties file to be packaged with my war so it is accessible when it is deployed to the server.
http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>java.net</id>
<name>Repository hosting the Java EE 6 artifacts</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>net.authorize</groupId>
<artifactId>java-anet-sdk</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<finalName>${artifactId}</finalName>
</build>
<profiles>
<profile>
<id>endorsed</id>
<activation>
<property>
<name>sun.boot.class.path</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- javaee6 contains upgrades of APIs contained within the JDK itself.
As such these need to be placed on the bootclasspath, rather than classpath of the
compiler.
If you don't make use of these new updated API, you can delete the profile.
On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.-->
<compilerArguments>
<bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
</properties>
Maven doesn't copy resources from the java source tree by default, but you can get it do that by adding this to your pom.xml:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes><exclude>**/*.java</exclude></excludes>
</resource>
</resources>
</build>
Make sure you exclude the java source files.
From https://rogerkeays.com/how-to-change-mavens-default-resource-folder
What is your project's build path configured to be in Netbeans? You might try changing it to src/main/webapp/WEB-INF/classes. This way class files compiled from your src/main/java folder and any resources you have under src/main/resources should get included in the generated WAR. You would then be able to access your config.properties file if you place it under the src/main/resources folder.
You might also review any includes sections in your pom.xml and ensure you're not accidentally excluding something (if you explicitly include some things, you're likely implicitly excluding everything else).
By default maven will include all files under resources folder. If your properties files are not in the resource folder, then you need to include the following in the pom.xml file under the build section.
<build>
/* other tags like <plugins> goes here */
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
/* other tags like <plugins> goes here */
</build>
Try putting your config.properties under src\main\resources\com\myapp. I was able to test this on a local project. I'm running Maven 3.0.2.
Created a mvn sample project with the webapp archetype:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
Created a directory at src/main/resources/com/foo and put a foo.properties file under it.
Ran a build:
mvn clean install
Then, when looking in the resulting target directory, the foo.properties file appears:
ls -al target/my-webapp/WEB-INF/classes/com/foo/
-rw-r--r-- 1 sblaes staff 4 Apr 2 22:09 foo.properties
You might try those steps on your machine. If that works, then start trying to simplify your POM above by removing things from it to see if it starts working. Trial and error is no fun, but I just don't see anything above that should be breaking it.
Huge gotcha for this:
when your resources are in "test/resources" (e.g. .properties files for tests)
maven doesn't copy them to target, so they're not in the classpath
Check whether your "packaging" is set to "pom" in the pom.xml:
<packaging>pom</packaging>
Fix is:
change your packaging to "jar" or "war" instead

Resources