How to configure maven resource plugin to complain when a property for resource filtering is not found? - maven

My question is simple: I want maven to give me some error messages when any property is not found during resource filtering.
I didn't find any clue in the official maven docs.
So how could I do this?

I would suggest to take a deep look at the maven-enforcer-plugin which can be used for such things:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>basedir</property>
<message>You must set a basedir property!</message>
<regex>.*\d.*</regex>
<regexMessage>The basedir property must contain at least one digit.</regexMessage>
</requireProperty>
<requireProperty>
<property>project.version</property>
<message>"Project version must be specified."</message>
<regex>.*(\d|-SNAPSHOT)$</regex>
<regexMessage>"Project version must end in a number or -SNAPSHOT."</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>

Related

Could I use maven and dependency-check-maven plugin to validate contens of ear file?

Is it possible to use maven and dependency-check-maven plugin to validate contens of already built ear file ? I'm trying something like below but I have no idea where I could point file which I want to verify
<build>
<plugins>
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<version>6.1.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
I've found resolution. I point directory under plugin level. It forces plugin to check all files placed there
<configuration>
<scanSet>
<fileSet>
<directory>\f1\f2\f3</directory>
</fileSet>
</scanSet>
</configuration>

Get the semantic versioning components within a Maven POM [duplicate]

Is it possible to get the major version (<Major>.<Minor>.<Patch>) of the project.version?
For example if my version is 1.3.4, I'd like to get 1 to later use it in a configuration of the same pom.xml
Something like:
<configuration>
<name>project_name.${project.version:major}</name>
</configuration>
If not, what are the alternatives?
Found it. The build-helper-maven-plugin has the ability to parse-out the components of the version.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>initialize</phase>
<id>parse-version</id>
<goals>
<goal>parse-version</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>[version] ${project.version}</echo>
<echo>[majorVersion] ${parsedVersion.majorVersion}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This works on Maven 3.3.9:
${project.artifact.selectedVersion.majorVersion}
Versions don't necessarily come in the structure you describe.
Maven has conventions for trailing numbers, but you don't have to use them.
If you have a convention that you like that you want to disassemble, you can write your own maven plugin that sets several properties to the several pieces as you define them.

Is there a way in maven to assure that a property is set

I just tracked down a difficult maven issue that was caused by a bad property value.
The property is a path to an alternate JVM that is used a run-time by a test.
I would like to make maven fail early by detecting if the path is valid or not.
What might be a way to accomplish this?
I plan to dig into antrun to see if there is a way to make it run first so that it can check, but that seems like overkill.
Question: How can I do this cleanly and simply?
You can use the Enforcer Maven Plugin and its Require Property rule, where you can enforce the existence of a certain property, optionally with a certain value (a matching regex), and fail the build otherwise.
This rule can enforce that a declared property is set and optionally evaluate it against a regular expression.
A simple snippet would be:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>basedir</property>
<message>You must set a basedir property!</message>
<regex>.*\d.*</regex>
<regexMessage>The basedir property must contain at least one digit.</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
Yes, you can use the maven-enforcer-plugin for this task. This plugin is used to enforce rules during the build and it has a built-in requireFilesExist rule:
This rule checks that the specified list of files exist.
The following configuration will enforce that the file ${project.build.outputDirectory}/foo.txt exists and will fail the build if it does not.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-files-exist</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${project.build.outputDirectory}/foo.txt</file>
</files>
</requireFilesExist>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
Use the Require Files Exist rule of the Maven Enforcer plugin.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-files-exist</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${property.to.check}</file>
</files>
</requireFilesExist>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Could not lookup required component: java.util.NoSuchElementException

I started testing a Maven plug-in via the maven-invoker-plugin, and am stuck with a weird exception:
[ERROR] Failed to execute goal my.company:plugin:1.0.4-SNAPSHOT:goal on project org.acme.simple: Could not lookup required component: java.util.NoSuchElementException
[ERROR] role: my.company.plugin.SomeClass
I added the maven-invoker-plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<pomIncludes>
<pomInclude>simple/pom.xml</pomInclude>
</pomIncludes>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<settingsFile>src/it/settings.xml</settingsFile>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I -erm- borrowed the settings.xml from this Maven plug-in. And what fails in the pom.xml to be tested is this call:
<plugin>
<groupId>my.company</groupId>
<artifactId>plugin</artifactId>
<version>#project.version#</version>
</plugin>
After some more digging around, I figure that Tycho is at least part of the problem:
<packaging>eclipse-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>0.22.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
If I remove #project.version# it works, but it's evidently not the current version of the plug-in that is tested. So I guess I have to leave it in. I tried adding maven-compat (as suggested here), but it didn't do anything.
The same exception is displayed when I don't add the plug-in in the pom.xml, but call it via:
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:goal
Any advice how to handle that problem?
Evidently this plug-in got lost somehow, adding it again made the exception go away:
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.5.5</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
<goal>generate-test-metadata</goal>
</goals>
</execution>
</executions>
</plugin>

Maven - Excluding Resource from a specific artifact

I'm new to maven, and found myself stuck with something which is really bothering me.
I have a multi-module project, and my parent pom.xml contains the following plugin:
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>
${shared.resources.version}
</resourceBundle>
</resourceBundles>
<includes>
<include>version.info</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
This code generates a version.info file and places them in each of my module jar files.
I was wondering if it's possible to make this code create the version.info file for only 2 modules out of the 3.
For example if I have modules: A, B and C.
I would like the version.info file to be in A and B but not in C.
I hope I explained myself well enough, in case not please let me know.
Thanks in advance,
Meny
Best solution would be moving this plugin configuration to the project/build/pluginManagement element of the parent pom:
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>
${shared.resources.version}
</resourceBundle>
</resourceBundles>
<includes>
<include>version.info</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
and call it at project/build/plugins in module poms:
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
</plugin>
</plugins>
if such refactoring of parent pom is not allowed then override plugin configuration with skip set into true or use the none phase to disable unwanted plugin calls:
http://thomaswabner.wordpress.com/2010/02/16/howto-disable-inherited-maven-plugin/

Resources