How to stop Maven from overwriting if the destination exists - maven

I have a directory in a dependency, that I want copied in src/main/webapp/mypath during the initialize phase.
But I want it to be copied exactly and only once, meaning that:
if src/main/webapp/mypath doesn't exist, then copy from dependency
if src/main/webapp/mypath exists, then never ever copy from dependency even if the one in the dependency is newer. If exists, don't overwrite it. Never.
I tried a couple of approaches with copy-resources and dependency:unpack but maven will always overwrite if mypath coming from the dependency is newer / updated even if I set to false every possible overwrite* configuration I'm aware of.
Any advice or RTFM + link to a manual I didn't read so far?

You can use profiles:
<profiles>
<profile>
<activation>
<file>
<missing>src/main/webapp/mypath</missing>
</file>
</activation>
... copy ...
</profile>
</profiles>

As described by #William, you can use ant plugin, export the property to maven context and skip the task if "true".
Here is the code:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<exportAntProperties>true</exportAntProperties>
<target>
<available file="src/main/resources/my-data" type="dir"
property="dir-exits"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-zip-dependencies</id>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<skip>${dir-exists}</skip>
<artifactItems>
<artifactItem>
<groupId>com.mygroup</groupId>
<artifactId>myartifactid</artifactId>
<includes>**/*.json</includes> <outputDirectory>src/main/resources/</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

Related

Maven plugin maven-antrun-plugin copy option does not overwrite

I have a problem with this maven plugin and I don't really know how to solve it.
I am trying to copy some resources to "${basedir}/../server/a/base-store" to "${basedir}/../resources/store/base_certificate_store_prod/base-store"
However I use "overwrite" on copy. But at the final end under "${basedir}/../server/a/base-store" the files are added and the others which were before still exist.
I would like the hole "base-store" directory to be replaced.
<profile>
<id>PROD</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-prod-base-store</id>
<phase>compile</phase>
<configuration>
<target>
<copy todir="${basedir}/../server/a" overwrite="true">
<fileset dir="${basedir}/../resources/store/base_certificate_store_prod" includes="**/*"/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Just delete the directory first and then copy.

Maven wildfly deployment with multiple servers (standalone)

in my environment I have two wildfly server where I want to deploy with the wildfly-maven-plugin.
The servers differ in the name dev01 and dev02 but have the same port 9993 and username and password.
My understanding is that the wildfly-maven-plugin support only single server deployment.
If the problem are not big enough we use a module/submodule structure where the war file will be build in a submodule.
I'm using two profiles wildfly-deploy-dev01 and wildfly-deploy-dev02.
<profiles>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>wildfly-deploy-dev02</id>
<build>
[...]
<profiles>
In the main module I skipped it.
In the war submodule:
<profiles>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
<finalName>${project.artifactId}-v1.0</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<id>wildfly-credentials<id>
<hostname>dev01.example.com</hostname>
<protocol>remote+https</protocol>
<port>9993</port>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>wildfly-deploy-dev01</id>
<build>
[same as above for hostname dev02.example.com]
</profiles>
First I was thinking everthing works fine but then I found out that only the last server will be deployed.
mvn wildfly:deploy -P wildfly-deploy-dev01,wildfly-deploy-dev02
I played around by setting the configration after the execution tag without success. It looks that the second profile overwrite the first one.
Futher I hardcoded the finalname because the parsedVersion is not parsed.
<finalName>${project.artifactId}-v${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</finalName>
At the moment I'm lost with Maven. Has anybody an idea how I can deploy with the plugin on two servers?
Thanks,
Markus
Ways which I tried:
https://github.com/tsotzolas/wildfly_maven_plugins_examples/blob/master/deployToMultiplesServer/pom.xml
wildfly-maven-plugin not deploying when multiple profiles selected
Cannot access parsedVersion value in pom properties
You should be able to do this in a single profile with different executions. There shouldn't be a need to multiple profiles.
<profiles>
<profile>
<id>wildfly-deploy</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<id>wildfly-credentials<id>
<protocol>remote+https</protocol>
<port>9993</port>
</configuration>
<executions>
<execution>
<id>deploy-dev1</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<hostname>dev01.example.com</hostname>
</configuration>
</execution>
<execution>
<id>deploy-dev2</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<hostname>dev02.example.com</hostname>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profiles>
With this you'd just have to do mvn clean install -Pwildfly-deploy.

How to trigger Maven SCM plugin to automatically switch goals based on existing directory?

I'm new to Maven and having an issue where I'm trying to automatically change the SCM plugin goal from checkout to update based on whether the source is already checked out.
Can anyone show me a code example to get this working?
This is the plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>checkout</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${project.basedir}/src</checkoutDirectory>
<workingDirectory>${project.basedir}/src</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
change goal:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>update</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${project.basedir}/src</checkoutDirectory>
<workingDirectory>${project.basedir}/src</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
Reference
https://maven.apache.org/scm/maven-scm-plugin/
https://maven.apache.org/scm/maven-scm-plugin/update-mojo.html
To change the goal of the SCM plugin was inspired by Đỗ Như Vý (above).
Approach was to
Place the goal in a property called scm.goal set to a default value
ie update.
Use a profile (bootstrap) to change the scm.goal property value from
'update' to 'checkout'.
Activate the bootstrap profile based on missing .gitignore file.
Place the property scm.goal in the SCM plugin
goal element.
Code:
<properties>
<scm.dest.path>${project.basedir}/src</scm.dest.path>
<scm.goal>update</scm.goal>
</properties>
<profiles>
<profile>
<id>bootstrap</id>
<activation>
<file>
<missing>./src/.gitignore</missing>
</file>
</activation>
<properties>
<scm.goal>checkout</scm.goal>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>${scm.goal}</goal>
</goals>
<configuration>
<connectionType>developerConnection</connectionType>
<scmVersion>master</scmVersion>
<scmVersionType>branch</scmVersionType>
<checkoutDirectory>${scm.dest.path}</checkoutDirectory>
<workingDirectory>${scm.dest.path}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
...

Maven extract Dependency without creating dependency directory

The configuration below works but I end up with an extra directory that I don't want. So I have target/webapp/dep-A/<depedency contents>, what do i need to change to get it to be target/webapp/<depedency contents>?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>myGroup</groupId>
<artifactId>dep-A</artifactId>
<version>1.0-SNAPSHOT</version>
<type>zip</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/webapp</outputDirectory>
</artifactItem>
</artifactItems>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
Unfortunately the maven-dependency-plugin unpack goal has not a postprocess optional parameter letting you, for example, chmod-ing or moving files. So I don't think exists an elegant way to do what you want only using the maven-dependency-plugin.
In addition to the maven-dependency-plugin you might use the maven-antrun-plugin, binding it to a subsequent phase which the maven-dependency-plugin is binded to, to copy/move/delete your files and directories.

Maven install:install-file seems not to work

I've created a Maven project with some dependencies on libraries (.jar files) which are not in Maven central. The internal repository that we had is being taken offline and we have not been authorized to make a new one, so I modified the POM to use install:install-file on each of those files when a certain profile is activated. It seemed to work on my machine, but it's possible it might not have worked because the .jars were already in my repository.
We had a new employee start this week and I was trying to help him install the dependencies by triggering the profile, and although the debug trace lists those goals as being on the plan (and mvn help:active-profiles shows the profile being activated), it doesn't work. The directories are created in the repository, but only a .lastupdated file for the .jar and the .pom are created.
The error we get is that POM could not be found for the given artifact. That's expected since we use <generatePom>true</generatePom> in the plugin's execution.
Any ideas on why this might not work?
Here's the relevant section of the POM:
<profiles>
<profile>
<id>self-contained</id>
<activation>
<property>
<name>installBundledJars</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>installCaseControlUpdateAPI</id>
<phase>generate-resources</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${basedir}/lib/CaseControlUpdateAPI.jar</file>
<groupId>com.west.thomson.contech</groupId>
<artifactId>CaseControlUpdateAPI</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
<execution>
<id>installMQ</id>
<phase>generate-resources</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${basedir}/lib/com.ibm.mq-5.304.jar</file>
<groupId>com.ibm</groupId>
<artifactId>mq</artifactId>
<version>5.304</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
<execution>
<id>installMQJMS</id>
<phase>generate-resources</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${basedir}/lib/com.ibm.mqjms-5.304.jar</file>
<groupId>com.ibm</groupId>
<artifactId>mqjms</artifactId>
<version>5.304</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
<execution>
<id>installJADABAS</id>
<phase>generate-resources</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${basedir}/lib/jadabas.jar</file>
<groupId>com.softwareag</groupId>
<artifactId>jadabas</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
<execution>
<id>installOJDBC</id>
<phase>generate-resources</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${basedir}/lib/ojdbc14.jar</file>
<groupId>oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10g</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Two possible problems i can think of,
- check if you have the libs in the specified path.
- check permissions on the .m2 repo and the project location where you are running mvn cmd.

Resources