Can I run maven plugin only if file changed after last build? - maven

We are using maven-exec-plugin during prepare resources phase to create binary file which is later packaged into jar. Exec launches script which reads excel sheet and creates sqlite db.
Now the script is run always, even if I don't run clean. How to configure plugin so it would run only when:
Output file does not exist.
OR Output file exist but last modification date is older then source file.

You may use <profile> activation to run the plugin only when target/afile.log does not exists :
<profiles>
<profile>
<id>run-exec</id>
<activation>
<file>
<missing>target/afile.log</missing>
</file>
</activation>
...
</profile>
</profiles>

Related

maven command line for running multiple profile

i have 2 profiles in my pom.xml and need to package 2 WAR files simultaneously
this is pom
<profiles>
<profile>
<id>TEST</id>
-- scripts
</profile>
<profile>
<id>DEV</id>
-- scripts
</profile>
</profiles>
i tried
clean package -P Dev,Test
but alywas the generated WAR is the last one (Test) and Dev profile not run
You can use multiple P arguments:
mvn clean package -P Dev -P Test
If you want to do that, you need to separate Maven runs.
After a lot of investigation i figure out it can't be done

How to activate a profile in a maven submodule using a property?

I am using Maven 3.6.0 and OpenJDK8 on Ubuntu 18.04 (also tested with Alpine Linux).
I have a pom.xml in the root of my project that includes my submodules :
...
<modules>
<module>mysubmodule</module>
</modules>
...
In the mysubmodule folder, the pom.xml has a profile that I want to activate based on a property passed to the mvn executable:
...
<profile>
<id>my-profile</id>
<activation>
<property>
<name>activateMyProfile</name>
</property>
</activation>
...
</profile>
...
I then execute mvn to start the build, but the profile is never activated:
If I run mvn -DactivateMyProfile release:prepare from the root of my project, the profile is never activated and never runs
If I run mvn release:prepare from the root of my project, the profile is never run.
I also tried the inverse:
...
<profile>
<id>my-profile</id>
<activation>
<property>
<name>!doNotActivateMyProfile</name>
</property>
</activation>
...
</profile>
...
If I run mvn -DdoNotActivateMyProfile release:prepare from the root of my project, the profile is still executed
If I run mvn release:prepare from the root of my project, the profile is also executed
It looks like mvn is not able to see the properties being passed through the command line. What is the correct way to activate a profile in a submodule using a property?
As I am using the maven release plugin, parameters must be passed using the -Darguments argument.
For example, instead of using mvn -DactivateMyProfile release:prepare, the correct invocation is: mvn -Darguments=-DactivateMyProfile release:prepare
If there are multiple arguments, use mvn -Darguments="-DactivateMyProfile -DsomeOtherArg -DanotherArg=something" release:prepare

Conditional processing in Maven projects

I have some configuration settings that are in the Maven project that I'd like to keep there for building into a newly set up system but not touched if the configuration setting already exists. One of the problems I had is that when I deleted the setting from the build, it also deleted the setting from the target server when I did a build.
What would good alternatives be?
A possible solution could be to move the particular set-up for the new systems in a Maven profile and activate the profile only based on either an environment variable or the existence/non-existence of a file.
For instance, from official documentation, you can activate the profile if a certain environment variable exists, not exists or exists with a certain value.
<profiles>
<profile>
<activation>
<property>
<name>environment</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
Please also note that, as from official documentation:
Note: Environment variables like FOO are available as properties of the form env.FOO. Further note that environment variable names are normalized to all upper-case on Windows.
Alternatively, the profile can be activated on a certain file (existing or missing), as following:
<profiles>
<profile>
<activation>
<file>
<missing>path/to/missing/file</missing>
</file>
</activation>
...
</profile>
</profiles>
If your set-up cannot be applied to any of these two cases, you could (as an example) anyway adapt it to create an harmless file which would then deny any further set-up.
harmless file missing > profile activated > set-up performed
harmless file existing > profile not activated

always download sources (and javadocs) from maven ant task

I am trying to get this ant-based project's init target to download all the sources and javadocs.
I added the following to my ~/.m2/settings.xml (as per Maven – Always download sources and javadocs) but it doesn't force source downloads when used from ant:
<profiles>
<profile>
<id>downloadSources</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<downloadSources>true</downloadSources>
</properties>
</profile>
</profiles>
The only way I could get the sources to download was by hacking build.xml so that all <artifact:dependencies> elements include sourcesFilesetId="sources.dependency.fileset", but this is a pretty distasteful commit that is unlikely to be accepted by the maintainers. A better solution would exist with a property file definition, preferably in the user's settings (not something that mutates the project definition)
Is there a simpler way to ensure that all the sources (and potentially javadocs) are globally downloaded in maven ant tasks?

How can I exclude a project from a mvn clean install? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I exclude certain modules from a maven build using the commandline
I am running a maven clean install in a pom file which includes several modules (and sub-modules). I was wondering if it is possible to run a maven build but specifying on command line to skip a module from the build ( at the moment I exclude them manually from the build, but Id prefer to do it via command line).
I know that with -pl you can selectively choose projects, but what I would like is to selectively exclude (in a blacklist fashion) some.
You could have a separate <modules> section in a profile, and activate the profile you need in the command line.
Example:
<profiles>
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>...</modules> <!-- module set 1 -->
</profile>
<profile>
<id>profile-2</id>
<modules>...</modules> <!-- module set 2 -->
</profile>
</profiles>
Now, dependent on your current need, execute
mvn install
mvn install -P profile-2
Note that you'd have to think it over carefully, there must be no cross-profile dependencies on the excluded module.

Resources