Log4j.properties not found using maven resources folder - java

I try to load a configuration for log4j using the PropertyConfigurator.configure("log4j.properties") method but I keep running into java.io.FileNotFoundException.
I followed this question and placed my log4j.properties file into the resources folder.
I Also edited my pom.xml like this :
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>src.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
So when I run mvn package the target folder generated contains my .jar and my log4j.properties side by side but when I run my jar I get the file not found exception.
How can I fix this issue ?

Note :
Please, DO NOT include the log4j.properties into the final Jar file, it will cause multiple log4j.properties files in the classpath, if someone is depending on your Jar, you may accidentally override their logging configurations, depends which Jar is loaded first.
For reference :
https://www.mkyong.com/maven/maven-exclude-log4j-properties-in-jar-file/

Related

Getting a logback.xml from a dependency jar and filtering it with the maven-war-plugin

We have a common logback.xml file that we would like to use across different web apps. It includes a RollinFileAppender which should see files named as the project artifactId. The logback.xml includes a property like so
<property name="LOG_FILE_NAME" value="$project.artifactId}"/>
Within our web project we would like to include a dependency e.g. logging-setup
<dependency>
<groupId>our.group.id</groupId>
<artifactId>logging-setup</artifactId>
</dependency>
How do we easily allow the maven-war-plugin to filter this file so that the ${project.artifactId} reference is replaced with the actual project.artifactId? I think it can be done using a combination of the maven-dependency-plugin and the maven-war-plugin something like below. However that would need to be included in every project POM. Is there an easier way?
Thanks,
Paul
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>our.group.id</groupId>
<artifactId>logging-setup</artifactId>
<version>${logging-setup.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
<includes>**/logback.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<targetPath>WEB-INF/classes</targetPath>
<includes>
<include>logback.xml</include>
</includes>
</resource>
</webResources>
<packagingExcludes>WEB-INF/lib/logging-setup-*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
After this logback.xml file is put inside an artifact jar file, it cannot be changed easily anymore during build time.
I would suggest a slightly different approach:
rename this file to e.g. logback-YOURCOMPANY.xml and change it so it can be included.
create a tiny logback.xml file put in each project as-is which sets the property as you do, and then includes logback-YOURCOMPANY.xml
enable filtering on this tiny logback.xml file so the variable is expanded during Maven build.
See https://logback.qos.ch/manual/configuration.html#fileInclusion for details.

Fitnesse error when used with spotify maven plugin and docker

I am using spotify maven plugin to create a fitnesse docker image and run it on a container. I am able to bring the fitnesse up and run the tests successfully locally without using spotify maven plugin and docker but not when I use those.
I get the following error when I start the fitnesse
Error message
Here is the contents of FrontPage fitnesse wiki which generally generally takes care of resolving dependencies as per http://blog.xebia.com/fitnesse-and-dependency-management-with-maven/
!contents
!define TEST_SYSTEM {slim}
!pomFile pom.xml
!note Release ${FITNESSE_VERSION}
Here is the contents of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadeTestJar>true</shadeTestJar>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>fitnesseMain.FitNesseMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<baseImage>${docker.registry.host.slash}mcpi/service</baseImage>
<entryPoint>["java","-jar","${serviceBin}/${finalJarName}.jar","-p","8000"]</entryPoint>
<imageName>mcpi/${project.name}</imageName>
<runs>
<run>mkdir -p ${serviceHome}</run>
</runs>
<workdir>${serviceHome}</workdir>
<resources>
<resource>
<targetPath>${serviceHome}</targetPath>
<directory>${basedir}/src/test/resources</directory>
</resource>
<resource>
<targetPath>${serviceBin}</targetPath>
<directory>${basedir}/target</directory>
<include>${finalJarName}.jar</include>
</resource>
<resource>
<targetPath>${serviceBin}</targetPath>
<directory>${basedir}/target</directory>
<include>${finalTestJarName}.jar</include>
</resource>
<resource>
<targetPath>${serviceBin}</targetPath>
<directory>${basedir}</directory>
<include>pom.xml</include>
</resource>
</resources>
</configuration>
</plugin>
I believe your problem might be that you don't include your maven settings and repository in the docker image, so the !pomFile does not work inside your docker image.
Having said that: you probably don't need it, since you bundle all your classes and dependencies (at least I assume that what the 'shade plugin' does for you). So you can probably disable the 'maven class path plugin, to prevent the problem you experience now. Disabling the maven classpath plugin can be done by adding -Dfitnesse.wikitext.widgets.MavenClasspathSymbolType.Disable=true to your Java command line starting FitNesse in docker (or by removing the line from the wiki page of course, but that impacts how you work locally).
But I don't know whether your tests will work immediately, or that you have to do something extra to ensure the generated 'final jar' is on the class path of the Java process that is started once an actual test is started (but you can try that locally by running with the !pomFile removed and starting from the jar created by the shade plugin).
Fitnesse was unable to parse pom.xml as maven is not set up in docker. Instead of !pomFile pom.xml in my fitnesse wiki, I used !path path/to/jars/*.jar.
Though above error was gone, maven-shade-plugin could not resolve all the dependencies like spring-test.
I had to add maven-dependency-plugin to pom.xml so that all the dependencies are resolved <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/dependencies</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>true</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
and moved those to docker using spotify docker-maven-plugin
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<baseImage>${docker.registry.host.slash}service</baseImage>
<entryPoint>["java","-jar","${serviceBin}/${finalJarName}.jar","-p","8000"]</entryPoint>
<imageName>mcpi/${project.name}</imageName>
<runs>
<run>mkdir -p ${serviceHome}</run>
</runs>
<workdir>${serviceHome}</workdir>
<resources>
<resource>
<targetPath>${serviceHome}</targetPath>
<directory>${basedir}/src/test/resources</directory>
</resource>
<resource>
<targetPath>${serviceBin}</targetPath>
<directory>${basedir}/target</directory>
<include>${finalJarName}.jar</include>
</resource>
<resource>
<targetPath>${serviceBin}</targetPath>
<directory>${basedir}/target</directory>
<include>${finalTestJarName}.jar</include>
</resource>
<resource>
<targetPath>${serviceBin}</targetPath>
<directory>${basedir}/target/dependencies</directory>
</resource>
</resources>
</configuration>
</plugin>

Leave FXML files in /src/main/java

Whenever I put FXML files into the /src/main/java catalog, it seems like they aren't being included into the final OSGi jar during compilation. I suppose Maven removes them from there because it thinks that FXML files should only reside in the /src/main/resouces catalog. Is there a way to stop Maven from doing this (i.e. just leave them there)?
EDIT 1
The only solution I have found for now is:
<build>
<plugins>
....
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/</outputDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
However, as far I understand, there is another, shorter way of achieving this, i.e. without the use of additional plugins, in the <build>...</build> node. How do I use the shorter method instead?
Yes by default src/main/resources is for resource files.
You can use maven resources plugin to override this behavior.
Here's an example. In your pom.xml
...
<build>
<resources>
<resource>
<targetPath>com/company/projectname</targetPath>
<directory>src/main/java/com/company/projectname</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
where <directory> is the source package which includes resource files(in your case xml files) and <targetPath> is the target where you want the resources to reside. You can omit <targetPath> if you are ok with having these resources in the root path of the jar.

Create runnable jar with maven 3.1 using maven-dependency-plugin doesn't create runnable jar

Using Maven 3.1
Eclipse Helios
Aspekt:
Try to create a runable jar file using maven-jar/dependency-plugins.
Problem:
After creating jar file and dependencies there are NoCLassDefFoundErrors when i try to start the jar file with command
java -jar program.jar
But the Class/File is available in ./dependency-jars folder???
I Also tried following command:
java -cp ./dependency-jars -jar program.jar
This didn't work, too.
Question:
Do you know why jvm can not find these classes? What is wrong?
pom.xml:
<build>
<finalName>program</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>dependency-jars/</classpathPrefix>
<mainClass>de.test.MainCLass</mainClass>
</manifest>
</archive>
<outputDirectory>${package-target-folder}</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<goals>
<goal>javadoc</goal>
</goals>
<phase>package</phase>
<id>create-javadoc</id>
<configuration>
<charset>UTF-8</charset>
<outputDirectory>${package-target-folder}/docs</outputDirectory>
<reportOutputDirectory>${package-target-folder}/docs</reportOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${package-target-folder}/dependency-jars/</outputDirectory>
</configuration>
</execution>
<execution>
</execution>
</executions>
</plugin>
</plugins>
</build>
[UPDATE]
The created MANIFEST.MF looks like
Manifest-Version: 1.0
Built-By: ahausden
Build-Jdk: 1.7.0_25
Class-Path: dependency-jars/spring-core-4.0.0.RELEASE.jar dependency-j
ars/commons-logging-1.1.1.jar dependency-jars/spring-jdbc-4.0.0.RELEA
SE.jar dependency-jars/spring-beans-4.0.0.RELEASE.jar dependency-jars
/spring-tx-4.0.0.RELEASE.jar dependency-jars/spring-oxm-4.0.0.RELEASE
.jar dependency-jars/spring-batch-core-2.2.0.RELEASE.jar dependency-j
ars/xstream-1.3.jar dependency-jars/xpp3_min-1.1.4c.jar dependency-ja
rs/jettison-1.1.jar dependency-jars/spring-aop-3.2.0.RELEASE.jar depe
ndency-jars/spring-context-3.2.0.RELEASE.jar dependency-jars/spring-e
xpression-3.2.0.RELEASE.jar dependency-jars/spring-batch-infrastructu
re-2.2.0.RELEASE.jar dependency-jars/spring-retry-1.0.2.RELEASE.jar d
ependency-jars/spring-batch-test-2.2.0.RELEASE.jar dependency-jars/co
mmons-io-1.4.jar dependency-jars/commons-dbcp-1.2.2.jar dependency-ja
rs/commons-pool-1.3.jar dependency-jars/commons-collections-3.2.jar d
ependency-jars/spring-test-4.0.0.RELEASE.jar dependency-jars/javax.in
ject-1.jar dependency-jars/slf4j-log4j12-1.6.1.jar dependency-jars/sl
f4j-api-1.6.1.jar dependency-jars/log4j-1.2.16.jar dependency-jars/sp
ring-orm-4.0.0.RELEASE.jar dependency-jars/aopalliance-1.0.jar depend
ency-jars/mariadb-java-client-1.1.1.jar dependency-jars/hibernate-cor
e-4.3.1.Final.jar dependency-jars/jboss-logging-3.1.3.GA.jar dependen
cy-jars/jboss-logging-annotations-1.2.0.Beta1.jar dependency-jars/jbo
ss-transaction-api_1.2_spec-1.0.0.Final.jar dependency-jars/dom4j-1.6
.1.jar dependency-jars/xml-apis-1.0.b2.jar dependency-jars/hibernate-
commons-annotations-4.0.4.Final.jar dependency-jars/hibernate-jpa-2.1
-api-1.0.0.Final.jar dependency-jars/javassist-3.18.1-GA.jar dependen
cy-jars/antlr-2.7.7.jar dependency-jars/jandex-1.1.0.Final.jar depend
ency-jars/hibernate-annotations-3.5.6-Final.jar dependency-jars/hiber
nate-commons-annotations-3.2.0.Final.jar dependency-jars/hibernate-jp
a-2.0-api-1.0.0.Final.jar dependency-jars/javassist-3.12.1.GA.jar dep
endency-jars/commons-cli-1.3-20140221.042048-103.jar
Created-By: Apache Maven 3.1.0
Main-Class: de.test.MainClass
Archiver-Version: Plexus Archiver
And the last "line" is as line separator. Seems that the MANIFEST.MF file looks a little bit corrupt, doesn't it?
Not sure what's going on, but this is what my pom looks like for building a runnable jar using shade:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>schema.xsd</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>stand-alone</shadedClassifierName>
<artifactSet>
<excludes>
<exclude>org.slf4j:slf4j-api:jar:</exclude>
<exclude>org.slf4j:slf4j-log4j12:jar:</exclude>
<exclude>org.slf4j:jcl-over-slf4j:jar:</exclude>
<exclude>commons-logging:commons-logging:jar:</exclude>
<exclude>commons-logging:commons-logging-api:jar:</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<finalName>MyFinalRunnableJarName</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>class.with.main.Method</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</plugin>
</plugins>
</build>
The simplest solution is to use maven-assembly-plugin like this:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<!-- NOTE: We don't need a groupId specification because the group is
org.apache.maven.plugins ...which is assumed by default.
-->
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
[...]
</project>
Try to provide entry into manifest file:
Main-Class: YourClassWithMainMethod
And also see this post. So you can make it manually and see the difference.
If dependency-jars is a directory also try:
java -cp ./dependency-jars/* -jar program.jar
I still cannot post comments, so please show what are these NoCLassDefFoundErrors.
And have you tried to do the same jar file manually from Eclipse and see the difference?
You have dependency dependency-jars/commons-cli-1.3-20140221.042048-103.jar in your manifest but you wrote you have SNAPSHOT version in directory. And there is only commons-cli:commons-cli:20040117.000000 in maven central.
#robermann please see this post (to work it should be in double quotes though)
Are you sure your java version, launched at command line, is equal or greater than that used by maven when compiling? try java -version
via command line, go to the parent dir of "dependency-jars" and run: java -jar program.jar
Ok, i solved the second/updated Problem:
I first startet to use eclipse with maven plugin to create the runable jar file. This runs in the described problems.
After all i tried to use maven 3.1 from console application and after all it works.
Seems that eclipse maven plugin has some problems.
Use this command to create jar file of any maven project.
mvn clean compile install

getResource() not locating the resource on classpath

I have set up basic maven project in java SE, with a resource:
main
-java
-resources
-config -> database.properties
now since I don't want this resource in the final jar, I define:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>config/database.properties</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>make-jar-ultimateParser</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<finalName>testApp</finalName>
<archive>
<compress>false</compress>
<!-- Manifest - MainClass & ClassPath -->
<manifest>
<mainClass>aa.bb.Class</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>config/database.properties</Class-Path>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<!-- Copy configuration files -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dist/config</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/config</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
So the resource is on classpath and the directory with the resource is copied to the final jar.
My manifest looks like this: "Class-Path: config/database.properties"
But I'm not able to read it:
String db = "/config/database.properties";
properties = new Properties();
properties.load(getClass().getResourceAsStream(db));
I tried absolute/relative paths, getClass()/ClassLoader. Nothing. It works flawlessly in NetBeans, but that's about it.
You're misunderstanding what the Maven Resources plugin does. It simply copies resources (perhaps with transformation) into the build output directory. Where they're then included into the build artifact (JAR, WAR, whatever).
To make this work (referencing the JAR's directory using the Class-Path manifest entry), you need to distribute the config file separately. Or reference it using a File, and not bothering with the classpath.
A better approach is to distribute your application as an assembly, which contains the core app, any dependencies, and the configuration file. This would typically be packaged as a ZIPfile, and the user would unzip it to install.
I dont understand why you are excluding it. Maven uses its own resources dir.
Project
|-- pom.xml
-- src
-- main
`-- resources
wich it makes easier the life the programmer. Theres no need to get classpatch or other when you using this way.

Categories

Resources