I am creating a jar out of my codebase with selectes packages. Also i need the xml files to be moved to META-INF folder. But i dont see that happening though my jar gets created.
<build>
<resources>
<resource>
<directory>${project.basedir}/src</directory>
<includes>
<include>jboss-ejb3.xml</include>
<include>ejb-jar.xml</include>
</includes>
<targetPath>${project.build.outputDirectory}/META-INF</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<includes>
<include>com/**/server/*EJB.class</include>
<include>com/**/common/*Remote.class</include>
</includes>
<!-- <archive> -->
<!-- <addMavenDescriptor>false</addMavenDescriptor> -->
<!-- </archive> -->
</configuration>
</plugin>
</plugins>
When i use Maven-resource-plugin i could see the META-INF folder generated in target folder. But not seeing the same inside jar.
This was solved by using <include>META-INF/**</include> as suggested by user M.Deinum
Related
I have these folders in my project structure
/src/main/java
/src/main/resources
In the "resources" folder I have 2 files: "config.properties" and "logging.properties".
And I have the following "" in my pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>myapp.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I run "mvn clean package", it does generate the "target" folder with the jar and the "classes" folder containg the properties file as mentioned above.
To read one of the properties files (after clicking on a Button), I'm using the following code:
Properties prop = new Properties();
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("logging.properties"));
String logFolder = prop.getProperty("myApp.property");
//Do something with 'logFolder'
It runs OK.
But if I change the "myApp.property" in "logging.properties" file, the change doesn't affect the "logFolder" value.
What must I do to be able to dinamically change the property value and make my application read the new value WITHOUT RECOMPILING THE PROJECT?
Thank you.
You can invoke respective plugins manually:
mvn resources:resources maven-assembly-plugin:single
Though this is not the best option anyway. It's better to eliminate re-packaging all together for local deploys:
Just start the app in IDE instead of building a JAR. IDE will detect changes in the configs.
And in general allow overriding your variables with either env vars or system variables. So after reading the file also check if the values are overriden and use those.
For remote deploys we usually don't keep configuration files in JAR (you don't want to keep PRD passwords in there, right?). So deploying to remote envs should use config files from restricted sources. This means that the app needs to be able to read configs that are not inside JAR.
Here's what I added to my pom file so Maven loads the resources into the jar at the root level.
<resources>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
<include>**/*.jpg</include>
</includes>
</resource>
</resources>
</build>
Notice how my folder, called my-resources, is not placed under main, it's placed under src. Of course, without telling Maven about your resource in the pom it won't do anything for you.
I didn't check your code, but there's how to tell Maven to pack your resources into your jar for you. And at a glance at your code, when working from inside a jar, you need to prefix your filename with a '/' because it's now using a relative path, not an absolute path.
getResourceAsStream("logging.properties") works outside a jar.
getResourceAsStream("/logging.properties") is what you need inside a jar.
I managed to solve this issue.
The other answers are correct. The real problem (I think) is with this "maven-assembly-plugin" that I was using to build.
For some reason, it does not read my properties files after build, maybe I was doing something wrong with it.
So, I changed my <build> script in my pom.xml to the following:
<build>
<finalName>myapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>logs</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>myApp.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/logs</include>
<include>**/*.properties</include>
<include>**/*.gif</include>
</includes>
</resource>
</resources>
</build>
In short: I replaced the "maven-assembly-plugin" with "maven-dependency-plugin" (I'm using an Oracle DB in this project), "maven-resources-plugin" (copying all of my resources to "/target" folder after build) and the "maven-jar-plugin" setting the "Class-path" property to ".".
This is my problem: I have a Maven project that is using a Swing library and it is a GUI app which I want to include a resources (images and one config.properties file) in my jar file.
Here is my pom.xml code:
<build>
<resources>
<resource>
<directory>${basedir}/resource</directory>
<includes>
<include>**/*.png</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/config</directory>
<includes>
<include>config.properties</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>.settings</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>bis.debug.mode.ui.MainBISDebugMode</mainClass>
</manifest>
</archive>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
and here is my structure to my project:
I included directories as it seen in the pom.xml and the files in it and still doesn't included in jar file and it doesn't see images on the GUI app. When I run the program as Java application everything is fine.
Is it possible to be something wrong in my code? How I can fix the problem?
You need only to put your config file in src/main/resources and no need to add resources in pom.xml because src/main/resources is the maven's default resources folder
I've got an issue with a maven project where I am distributing dlls from the src/main/resources/lib folder.
The project is built as a single jar with dependencies using the maven-assembly-plugin.
Unfortunately, the maven process is corrupting my dll libraries during the copy process so that they are no longer useful to the application.
I've had a look at such concepts as resource filtering.
Here's my relevant pom.xml
Does anyone have any ideas?
I think I need to do something like this but so far it's not working for me.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<resources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
<archive>
<manifest>
<mainClass>de.bochumuniruhr.psy.bio.behaviourcoder.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
My final solution (based on the answers below):
Thank you for the great answers. I ended up going with the answer that doesn't require extending the configuration of the maven-resources-plugin. I placed my binary files in the src/main/resources/unfiltered-resources folder as I needed to filter my other resources.
Here is a link to the source code.
Below is my final working pom at the time of writing.
<build>
<finalName>BehaviourCoder_${git.build.time}_${git.commit.id.describe-short}</finalName>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${basedir}/src/main/unfiltered-resources</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.2</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<injectAllReactorProjects>true</injectAllReactorProjects>
<dateFormat>yyyy-MM-dd_HHmmss</dateFormat>
<dateFormatTimeZone>UTC</dateFormatTimeZone>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>de.bochumuniruhr.psy.bio.behaviourcoder.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
This part:
<resources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
Should be under under the <build/> section like this:
<project>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
...
</plugins>
</build>
</project>
When assembly plugin kicks in it is already too late, as the resources were already copied by maven resources plugin. You should exclude filtering on earlier phase (when the resources are being copied to target folder by maven resource plugin).
See maven's docs how to do this: https://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html
For your case this can be something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>dll</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
I am having problems getting Maven to build my webapp without including extraneous development file, such as unminified script and css files.
First i tried using exclude in combination with webResources
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>ReportRocket-1</warName>
<webResources>
<resource>
<directory>src/main/webapp/resources/</directory>
<excludes>
<exclude>annotated-js/*.js</exclude>
<exclude>compiled-js/*.js</exclude>
<exclude>css/*.css</exclude>
<exclude>less/*.less/exclude>
</excludes>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>connection.json</exclude>
<exclude>reportRocket.jsp</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
The result was the contents of WEB-INF being duplicated in the project root and no excluded directories or files.
So, I looked around here and found this: maven2: excluding directory from WAR but running mvn clean package using either warSourceExcludes or packagingExcludes results in the directories i'm trying to exclude not being, well, excluded...
The build section of my pom.xml
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>ReportRocket-1</warName>
<packagingExcludes>
src/main/webapp/resources/annotated-js/,
src/main/webapp/resources/compiled-js/,
src/main/webapp/resources/css/,
src/main/webapp/resources/less/,
src/main/webapp/WEB-INF/connection.json
</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
Building the project with these settings results in the following project structure:
META-INF
resources
//desired folders
annotated-js
compiled-js
css
less
WEB-INF
// desired files
connection.json
This is my first time using a build tool, so i'm probably overlooking something simple but in the meantime, this is driving me crazy. Any suggestions or obvious problems with my xml?
First you should read the documentation of the maven-war-plugin cause the packagingExclude is intended for a different purpose, but in your case you need to do the configuration in that way:
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp/resources/</directory>
<!-- there's no default value for this -->
<excludes>
<exclude>annotated-js/**</exclude>
</excludes>
</resource>
</webResources>
</configuration>
I have a
src/main/resources/log4j.xml
file which I would like to be included in the jar when I perform a mvn assembly:assembly but excluded when I perform a mvn package. How could I do this?
I have read several maven docs and questions here but I've been unable so far to do this. I tried many approaches, all of them failed.
Basically, I have in the <build><plugins> section of my pom:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
and then in the <build> section
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>src/main/resources/log4j.xml</exclude>
</excludes>
</resource>
</resources>
but I get log4j.xml included everywhere.
Anything you place in the <exclude> or <include> elements is treated as relative to the <directory> specified. So, they way you have it currently, Maven will try to exclude resource ${basedir}/src/main/resources/src/main/resources/log4j.xml which probably does not exist. Try setting the resources element like this:
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/log4j.xml</exclude>
</excludes>
</resource>
As a side note, the assembly:assembly goal is deprecated. Per the maven-assembly-plugin docs, use the single goal instead.
org.apache.maven.plugins works fine, simple control over including or excluding files in package phase!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>**/password.properties</exclude>
</excludes>
</configuration>
</plugin>