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

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

Related

Not include src/lib/*.jar in war file

I want when run
mvn verify
to include all files from /lib/ folder to war archive in folder 'web-inf/lib`
So here my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myhost</groupId>
<artifactId>sailero</artifactId>
<name>myapp</name>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.host</groupId>
<artifactId>some-lib</artifactId>
<scope>system</scope>
<version>0.0.1</version>
<systemPath>${project.basedir}/lib/some-lib-0.0.1.jar</systemPath>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<configuration>
<packagingIncludes>lib/*.jar</packagingIncludes>
</configuration>
</configuration>
</plugin>
in console:
mvn verify
But in war NOT INCLUDE some-lib-0.0.1.jar
in war in \target\myapp-1.0-SNAPSHOT.war\WEB-INF\lib\ -> not exist "some-lib-0.0.1.jar", but all other dependencies included.
Maven war plugin automatically copies all the project dependencies into WEB-INF/lib. So if your war needs a dependency just put the relevant GAV into the dependency section of this module.
Example:
In your pom, you have a dependency on:
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.7.0</version>
</dependency>
So it will be automatically included appear in WEB-INF/lib of your WAR.
Now it doesn't work like this with dependencies in scope system and this is a root cause of the issue here.
Long story short, this question has been already asked/answered in SO.
Bottom line, I suggest getting rid of system dependency and placing it at least in the local repo or ideally in some proxy like Nexus or Artifactory. But of course, you're welcome to test other approaches suggested in the provided link.
This fix problem:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webResources>
<resource>
<directory>lib</directory>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
Here correct approach:
first install "some-lib-0.0.1.jar" in local maven repo:
mvn install:install-file -Dfile=myproject\lib\some-lib-0.0.1 -DgroupId=com.host -DartifactId=some-lib -Dversion=0.0.1 -Dpackaging=jar
and second in pom.xml
<dependency>
<groupId>com.host</groupId>
<artifactId>some-lib</artifactId>
<version>0.0.1</version>
</dependency>
Also I found another approach:
Create a “lib” folder under your project like this: “\src\main\webapp\WEB-INF\lib”
Copy needed “jars” etc that you want included inside your WAR bundle folder.
Invoke your maven build as you normally do. I use “mvn install”, which creates builds the war file

Compilation error in building maven project

we are trying to build and deploy a maven artifact into our Nexus Repository Manager from Jenkins, But we are facing compilation error during the build.
This is the error that we are getting.!
ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile
(default-compile) on project studentapp: Compilation failure ERROR] No
compiler is provided in this environment. Perhaps you are running on a
JRE rather than a JDK? [ERROR] COMPILATION ERROR : [INFO]
------------------------------------------------------------- [ERROR] No compiler is provided in this environment. Perhaps you are running
on a JRE rather than a JDK?
This is our pom.xml file.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jdevs</groupId>
<artifactId>studentapp</artifactId>
<version>2.5-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>deployment</id>
<name>Internal Releases</name>
<url>http://rig.eastus.cloudapp.azure.com:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>deployment</id>
<name>Internal Snapshot Releases</name>
<url>http://rig.eastus.cloudapp.azure.com:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-webdav</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-guice</artifactId>
<version>2.1.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Reconfigure your Jenkins . From home page goto Manage Plugins and then goto Global Tool Configuration. Reintialize JAVA_HOME field with the correct path to JDK folder.
you need to have JDK install paths defined in your Jenkins configuration. Also if you are using master slave configuration you need to make sure that the particular has the same JDK installed (on the same location) as defined in Jenkins configuration.
Same goes for ANT and Maven installations also.
At least two other sources indicate that, with remote agents, you need to create an environment variable in the node configuration for java.home pointing to the JAVA_HOME directory (not simply %JAVA_HOME% or similar).
Navigate to: Jenkins > Manage Jenkins > Manage Nodes and click the configure icon next to your remote Node. From there, scroll down to Node Properties, tick the box for Environment Variables and define java.home for name and the path to your JDK for the value, e.g. c:\Program Files\Java\jdk1.8.0_181
This was what I eventually needed to do, even after all system environment variables were found to be correct on the build agent server and both java -version and javac -version were correctly reporting.
I realize the OP doesn't specify Master or Remote, but hopefully this is helpful to someone who stumbles upon this Q & A.
This source provided the first clue but no explanation: https://www.pgs-soft.com/blog/oops-jenkins-slave-maven-not-working/
I'm unable to track down the other mention of this fix I found while googling.

Setting a custom icon on javafx application

I'm trying to build a native bundle on macos and add a custom icon instead of default java.
I put my 128x128 icns file in src/main/deploy/package/macosx, add javafx-maven-plugin in pom.xml and run command mvn jfx:build-native (or mvn jfx:native). And nothing happens. Meaning it is successfully built, but still uses default icon. What am I'm doing wrong?
Here is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>StarWars</groupId>
<artifactId>Minesweeper</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>example.minesweeper.Main</mainClass>
<application.title>${project.artifactId}</application.title>
<copyright>Han Solo</copyright>
</properties>
<organization>
<name>Star Wars</name>
</organization>
<name>Minesweeper</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-core</artifactId>
<version>4.0.6-alpha</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-junit</artifactId>
<version>4.0.6-alpha</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<vendor>AndreySerebryanskiy</vendor>
<mainClass>example.minesweeper.Main</mainClass>
<deployDir>${project.basedir}/src/main/deploy</deployDir>
</configuration>
</plugin>
</plugins>
</build>
</project>
And here is a screenshot of my project structure.
By the way, I'm using IntelliJ IDEA 2017.1.4. I also have seen this range of questions:
JavaFX native bundle icon OS X
including an icon into a self-contained JavaFX application (.exe)
I also tried to put package/macosx in the project folder but it didn't work.
How to set custom icon for javafx native package icon on Windows
I also saw a simple way of accomplishing this task in NetBeansIDE (for example here). But I do not want to change IDE because of such simple issue.
Thank you in advance!
Edit 31.07
After turning verbose mode on I found that it was expecting Minesweeper-1.0.icns instead of Minesweeper.icns. Adding appName field in conficurations of javafx-maven-plugin solved my problem.
You will have to add to your javafx-maven-plugin configuration.
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<vendor>AndreySerebryanskiy</vendor>
<mainClass>example.minesweeper.Main</mainClass>
<deployDir>${project.basedir}/src/main/deploy</deployDir>
<verbose>true</verbose><!-- always a good idea to have this set to true while debugging -->
<appName>Minesweeper</appName>
</configuration>
</plugin>
Please make sure your ICNS-filename is the same as inside <appName>, then just call mvn jfx:native to have your application bundled.

How to download spring framework zip file [duplicate]

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
SpringSource.org changed their site to http://spring.io
Does someone know how to get the latest build without Maven/github? from http://spring.io/projects
Please edit to keep this list of mirrors current
I found this maven repo where you could download from directly a zip file containing all the jars you need.
https://maven.springframework.org/release/org/springframework/spring/
https://repo.spring.io/release/org/springframework/spring/
Alternate solution: Maven
The solution I prefer is using Maven, it is easy and you don't have to download each jar alone. You can do it with the following steps:
Create an empty folder anywhere with any name you prefer, for example spring-source
Create a new file named pom.xml
Copy the xml below into this file
Open the spring-source folder in your console
Run mvn install
After download finished, you'll find spring jars in /spring-source/target/dependencies
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-source-download</groupId>
<artifactId>SpringDependencies</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>download-dependencies</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Also, if you need to download any other spring project, just copy the dependency configuration from its corresponding web page.
For example, if you want to download Spring Web Flow jars, go to its web page, and add its dependency configuration to the pom.xml dependencies, then run mvn install again.
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>

Can't get EAR package structure correctly with Maven

I got a simple maven project and yet frustratingly fail for hours to get it right. The project contains 1 parent module, and 2 submodules (one for ear-packaging, the other for an ejb). Building works successfully, but the ear-packing just doesn't work as expected:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended</artifactId>
<version>1.0</version>
</parent>
<artifactId>extended-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended-ejb</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.7</version>
<configuration>
<version>6</version>
<generateApplicationXml>false</generateApplicationXml>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<jarModule>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended-ejb</artifactId>
<bundleDir>/</bundleDir>
</jarModule>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now when I look into the packaged folder I see this structure:
|-lib
extended-ejb-1.0.jar
activation-1.1.jar
javaee-api-7.0.jar
javax.mail-1.5.0.jar
|-META-INF
application.xml
What I expected however was this structure:
|-extended-ejb-1.0.jar
|-lib
activation-1.1.jar
javaee-api-7.0.jar
javax.mail-1.5.0.jar
|-META-INF
application.xml
So basically I wanted the ejb outside of the other libraries. These other 3 libraries come from the ejb-module which requires the javaee-api dependency for annotations. Unfortunately it also collects transitive the javax.mail.jar, activation.jar.
Now I really don't know why the structure just doesn't work as expected, by all means I tried to follow this guide step by step.
Ok, after I checked out a project from various maven archetypes I found the error... the minimal error... it's always a minimal error costing huge amounts of time...
In the dependencies section of the ear file, where I define my ejb as dependency, just add this:
<type>ejb</type>
so it's:
<dependency>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended-ejb</artifactId>
<version>1.0</version>
<type>ejb</type>
</dependency>
Afterwards it works correctly. Obviously it is wrong on the IBM page and on many other pages. Besides I found out the following section is also outdated, and can be completely removed from the ear-plugin section:
<jarModule>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended-ejb</artifactId>
<bundleDir>/</bundleDir>
</jarModule>

Resources