Maven Doesnot Overwrite files in target folder - java

I am using maven for build, downloading war from artifactory and in src/main/webapp/WEB-INF/ folder customized one of the file but in target//WEB_INF/ the file is not overwriting with the customized file in the final war.
I used true in maven resources plugin.
EG:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-app</id>
<!-- <phase>process-resources</phase> -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.artifactId}-${project.version}/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp/WEB_INF</directory>
<includes>
<include>app.xml</include></includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

First run mvn clean which will remove target, and then run mvn install should create artifact with latest changes.
if mvn clean is not removing target folder, check if there are any access issues

Related

Maven artifact plugin buildinfo remove unwanted buildinfo information

On using
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-artifact-plugin</artifactId>
<version>3.3.0</version>
</plugin>
I am able to get buildinfo file generated as following as per https://reproducible-builds.org/docs/jvm/
#### Work In Progress ####
buildinfo.version=1.0-SNAPSHOT
name=name
group-id=groupId
artifact-id=artifact id
version=version
**source information**
no scm configured in pom.xml
**build instructions**
build-tool=mvn
**effective build environment information**
java.version=11
java.vendor=Oracle corporation
os.name=Linux
**Maven rebuild instructions and effective environment:**
mvn.version=Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
**output**
outputs.0.filename=pom file name
outputs.0.length=<1234
outputs.0.checksums.sha512=abcd
Apart from name,group-id,artifact-id and version, i don't want other properties to be generated in that file. how can i configure that using maven-artifact-plugin
I have checked the doc https://maven.apache.org/plugins/maven-artifact-plugin/plugin-info.html, couldn't find example of removing unwanted information from getting generated in that file.
You may achieve something similar using maven-resources-plugin and optionally copy-rename-maven-plugin (if you need to get particular name of buildinfo file)
contents of .buildinfo:
name=${project.name}
group-id=${project.groupId}
artifact-id=${project.artifactId}
version=${project.version}
maven configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}</directory>
<filtering>true</filtering>
<includes>
<include>.buildinfo</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-and-rename-file</id>
<phase>process-resources</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/.buildinfo</sourceFile>
<destinationFile>${project.build.directory}/${project.artifactId}-${project.version}.buildinfo</destinationFile>
</configuration>
</execution>
</executions>
</plugin>

Additional resources directories for images in Maven site

For a Maven site, the standard image directory is src/site/resources/images.
Unfortunately, my asciidoc editor copies images to src/site/asciidoc/images. Can I somehow add this directory to the site resources (as in the Maven resources plugin)?
To copy all resources to an outputDirectory, you can simply specify following in your pom.xml-
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/site/resources/images</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Sources - Example from the plugin itself.

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>

How to add multiple .properties files as build artifacts with Maven?

I have a Maven project where I want to have two build artifacts:
The jar file containing the compiled Java source.
A folder containing a number of .properties file.
How can I setup my Maven project to do this? And then, once I've done this, how can I consume them up the dependency graph?
Add a copy-resources goal of the Maven Resources Plugin to your POM.
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-property-files</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/property-files</outputDirectory>
<resources>
<resource>
...
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
I can't understand what you mean exactly by "consume them up the dependency graph".

Specify common resources in a multi-module maven project

Is there any way to share resources between modules of a parent project in Maven? For example, I would like to specify one log4j.properties file for all the modules in a multi-module Maven project.
Generally, I use Eclipse IDE to create the parent project by choosing a general project and then convert it to a Maven project by specifying a packaging of pom. This creates a "clean" project structure without src and etc. folders. I wonder where such a shared resource should be put in this case.
EDIT1: I would like to put the common resources in the parent project.
I'd create one additional "base" module (project), packaging "jar", that contains the common resources in src/main/resources. Then I'd make the other modules depend on that project. Now they see the common resources on their classpaths.
Antoher possibility is to use a remote resource bundle. You would be able to configure it in the parent project. In this example I wanted to copy some files just for tests. If you use this you will need to create the bundle in another project.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>es.sca:myBundle:1.0.0</resourceBundle>
</resourceBundles>
<attachToMain>false</attachToMain>
<attachToTest>true</attachToTest>
<appendedResourcesDirectory>${basedir}/src/test/resources</appendedResourcesDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
Another way, put in your project root pom:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<!-- don't propagate to child poms -->
<!-- this will only execute in root pom -->
<inherited>false</inherited>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<!-- don't add classifier -->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugins>
And example of assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>resources</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/resources/</directory>
<outputDirectory/>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Assembly plugin will generate artifact and attach it to current reactor, so it will be installed and deployed.
No you can use it as standard dependency event in the same pom.
Important is to trigger assembly (proper phase) before another plugin which will use generated artifact.
Eg. You can have in your root pom, bellow configuration will be propagated to all your module:
<plugin>
<artifactId>some-maven-plugin</artifactId>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>goal</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>your.project.groupid</groupId>
<artifactI>your.project.artifactId</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
You can see this method in project:
https://github.com/s4u/pgp-keys-map resources directory is shared by all module.
Yes, it seems as a possible solution. But I was interested whether it
is possible to specify these resources in the parent project (without
introducing additional module) since the parent project specifies all
the common dependencies and Maven configurations for the child
modules, I think that the parent project is the most suitable place
also for the common resources.
In case of packaging type pom , when goal package specified to manage your shared resources, just add next (check folders) into build section of pom file :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config-files</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/logconfig</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I think you can just add the resources and/or testResources elements to your pom.
E.g. to access an additional test resource directory add:
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>../global/src/test/resources</directory>
</testResource>
</testResources>
see Maven - Override test resource folder
I managed it to work like this:
I create a project/assembly/test/resources/META-INF/persistence.xml file, and add this to my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-test-persistence-xml-resources</id>
<phase>process-test-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/</outputDirectory>
<resources>
<resource>
<directory>${project.parent.basedir}/assembly/</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources