how to fail maven build on dependency conflict - maven-2

Im working on a large multi-module project which uses an internal framework as one of its dependencies. The framework version is set in the top level pom at the beginning of a project, and has to stay constant. If any submodule uses a different version, I want the build to fail.
Ive tried declaring the dependency as a single version:
<dependency>
<groupId>framework_snt</groupId>
<artifactId>SFP</artifactId>
<version>[6.1]</version>
<type>pom</type>
</dependency>
Ive tried using the enforcer plugin with banned dependencies:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>[1.6.0-21]</version>
</requireJavaVersion>
<requireMavenVersion>
<version>[3.0.3]</version>
</requireMavenVersion>
<bannedDependencies>
<excludes>
<exclude>framework_snt:SFP</exclude>
</excludes>
<includes>
<include>framework_snt:SFP:6.1.2</include>
</includes>
</bannedDependencies>
</rules>
</configuration>
</execution>
</executions>
</plugin>
Ive also tried adding the <DependencyConvergence/> tag as mentioned here, but none of these approaches work.
So given this top level pom fragment:
<project>
<groupId>glb</groupId>
<artifactId>GLB</artifactId>
<packaging>pom</packaging>
<name>Global</name>
<version>1.0</version>
........
<dependencies>
<dependency>
<groupId>framework_snt</groupId>
<artifactId>SFP</artifactId>
<version>[6.1.2]</version>
<type>pom</type>
</dependency>
</dependencies>
.....
</project>
And this (invalid) submmodule:
<project>
<groupId>glb</groupId>
<artifactId>CORE</artifactId>
<packaging>jar</packaging>
<name>Core</name>
<version>1.0</version>
<parent>
<groupId>glb</groupId>
<artifactId>GLB</artifactId>
<version>1.0</version>
</parent>
<dependencies>
<dependency>
<groupId>framework_snt</groupId>
<artifactId>SFP</artifactId>
<version>6.3</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
how do I setup maven so the build will fail when using the child module above, but when I remove the tag <version>6.3</version> it succeeds (or I change the version to match the one from the top level pom?

Well after much search and trying stuff with various plugins and getting nowhere, I decided to write my own plugin to do conflict checking. Using the dependency:tree plugin source as a base I wrote the following:
/**
* Walks the dependency tree looking for anything marked as a conflict, and fails the build if one is found
* code ripped from the dependency:tree plugin
*
* #param rootNode
* the dependency tree root node to check
*
*/
private void checkForConflicts( DependencyNode rootNode ) throws MojoExecutionException
{
StringWriter writer = new StringWriter();
boolean conflicts=false;
// build the tree
DependencyNodeVisitor visitor = new SerializingDependencyNodeVisitor( writer, SerializingDependencyNodeVisitor.STANDARD_TOKENS);
visitor = new BuildingDependencyNodeVisitor( visitor );
rootNode.accept( visitor );
getLog().debug("Root: "+rootNode.toNodeString() );
DependencyNode node;
Artifact actual, conflict;
DependencyTreeInverseIterator iter = new DependencyTreeInverseIterator(rootNode);
while (iter.hasNext() )
{
node = (DependencyNode)iter.next();
if (node.getState() == DependencyNode.OMITTED_FOR_CONFLICT)
{
actual = node.getArtifact();
conflict = node.getRelatedArtifact();
getLog().debug("conflict node: " + node.toNodeString() );
getLog().debug("conflict with: "+conflict.toString() );
getLog().error(actual.getGroupId()+":"+actual.getArtifactId()+":"+actual.getType()+" Conflicting versions: "+actual.getVersion()+" and "+conflict.getVersion() );
conflicts=true;
}
}
if (conflicts)
throw new MojoExecutionException( "version conflict detected");
}
You can call this from your existing plugin using the following:
try
{
rootNode = dependencyTreeBuilder.buildDependencyTree( project, localRepository, artifactFactory, artifactMetadataSource, null, artifactCollector );
checkForConflicts( rootNode );
}
catch ( DependencyTreeBuilderException e)
{
throw new MojoExecutionException( "Cannot build project dependency tree", e);
}
Then in your plugin pom.xml, include dependencies from the existing dependency plugin source and your good to go.
This works for our project -- but if anyone knows a cleaner or better way, please let me know.

Related

Unable to resolve class - Groovy junit testing using maven

I am trying to create groovy tests where i use other methods from other Groovy scripts. Whenever i run mvn test i get unable to resolve class error, referring to the illegal import from another groovy script.
This is the testclass that I've written :
import jenkins_methods.gitMethods
import org.junit.Test
class GitMethodsTest extends GroovyTestCase{
GitMethodsTest(){
}
def gitMethods = new gitMethods()
#Test
void testGetDvhBranches(){
try{
gitMethods.getDVH_branches()
}
catch(Exception e){
println e
}
}
}
gitMethods.groovy
package jenkins_methods
// src/jobMethods
import dvh.util.AnsiColorDefinition
import jenkins_methods.fileMethods
String getGitDVH_BranchPath_flash() { return '/git-repository1/git/dvh/branches/' }
String dvhBranchNull() { return 'feature/DV-null-repo' }
String getDVH_branches() {
gitURL_DVH= "censored_url"
String response = sh returnStdout: true, script: "git ls-remote -h ${gitURL_DVH()}"
def branches = response.replaceAll(/[a-z0-9]*\trefs\/heads\//, ',')
branches = branches.substring(1, branches.length()).replaceAll("\n", "").replaceAll("\r", "")
return branches
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>jenkins-jobs</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<configuration>
<providerSelection>2.0</providerSelection>
</configuration>
<goals>
<goal>addSource</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The jenkins_methods package contains various groovy scripts that are necessary for the Tests to become relevant.
Structure of the project is as following :
-src
-test
-groovy
-GitMethodsTest.groovy
-jenkins_methods
-gitMethods.groovy
This is the error that i get when i run mvn test
[ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.5:testCompile (default) on project jenkins-jobs: startup failed:
[ERROR] /C:/Users/jenkins-jobs/src/test/groovy/GitMethodsTest.groovy: 3: unable to resolve class jenkins_methods.gitMethods
[ERROR] # line 3, column 1.
[ERROR] import jenkins_methods.gitMethods
[ERROR] ^
[ERROR]
[ERROR] 1 error
How do i solve this problem?

Cucumber: undefined scenario/steps when running a bunch of test with tags (annotations) using POM.xml

I am facing a problem when executing cucumber scripts having tags using POM.XML. I wish to run it without a runner class.
When I use the running class, the scripts run well and I get the below output.
/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=51160:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/junit/lib/junit5-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/tools.jar:/Users/umeshanand/Documents/Selenium/Frameworks/FW.BDD/target/test-classes:/Users/umeshanand/.m2/repository/info/cukes/cucumber-java/1.2.2/cucumber-java-1.2.2.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-java/3.7.1/selenium-java-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-api/3.7.1/selenium-api-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-chrome-driver/3.7.1/selenium-chrome-driver-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-edge-driver/3.7.1/selenium-edge-driver-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-firefox-driver/3.7.1/selenium-firefox-driver-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-ie-driver/3.7.1/selenium-ie-driver-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-opera-driver/3.7.1/selenium-opera-driver-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-remote-driver/3.7.1/selenium-remote-driver-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-safari-driver/3.7.1/selenium-safari-driver-3.7.1.jar:/Users/umeshanand/.m2/repository/org/seleniumhq/selenium/selenium-support/3.7.1/selenium-support-3.7.1.jar:/Users/umeshanand/.m2/repository/net/bytebuddy/byte-buddy/1.7.5/byte-buddy-1.7.5.jar:/Users/umeshanand/.m2/repository/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar:/Users/umeshanand/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar:/Users/umeshanand/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/Users/umeshanand/.m2/repository/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar:/Users/umeshanand/.m2/repository/com/google/guava/guava/23.0/guava-23.0.jar:/Users/umeshanand/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/umeshanand/.m2/repository/com/google/errorprone/error_prone_annotations/2.0.18/error_prone_annotations-2.0.18.jar:/Users/umeshanand/.m2/repository/com/google/j2objc/j2objc-annotations/1.1/j2objc-annotations-1.1.jar:/Users/umeshanand/.m2/repository/org/codehaus/mojo/animal-sniffer-annotations/1.14/animal-sniffer-annotations-1.14.jar:/Users/umeshanand/.m2/repository/org/apache/httpcomponents/httpclient/4.5.3/httpclient-4.5.3.jar:/Users/umeshanand/.m2/repository/org/apache/httpcomponents/httpcore/4.4.6/httpcore-4.4.6.jar:/Users/umeshanand/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/umeshanand/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/umeshanand/.m2/repository/info/cukes/cucumber-core/1.2.2/cucumber-core-1.2.2.jar:/Users/umeshanand/.m2/repository/info/cukes/cucumber-html/0.2.3/cucumber-html-0.2.3.jar:/Users/umeshanand/.m2/repository/info/cukes/cucumber-jvm-deps/1.0.3/cucumber-jvm-deps-1.0.3.jar:/Users/umeshanand/.m2/repository/info/cukes/gherkin/2.12.2/gherkin-2.12.2.jar:/Users/umeshanand/.m2/repository/info/cukes/cucumber-junit/1.2.2/cucumber-junit-1.2.2.jar:test/java/.:src/test/java/stepDefs" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 runner.runnertest
objc[976]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x102c0d4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10352b4e0). One of the two will be used. Which one is undefined.
Launch browser
open google
enter powercor
validate results
Launch browser
open google
enter powercor
validate results
Launch browser
open google
enter powercor
validate results
3 Scenarios (3 passed)
12 Steps (12 passed)
0m0.125s
When I execute using a POM.XML, I get the below message (please note I am trying to execute one of the three tags #google1, #google2, #google3)
--- exec-maven-plugin:1.2.1:java (default) # FW_BDD ---
Feature: First test to launch google
#google1
Scenario: Launch Google and search something # features/googlenew.feature:4
Given I open Chrome browser
And user open google
When user enter powercor in textbox
Then search results should should poercor.com.au
1 Scenarios (1 undefined)
4 Steps (4 undefined)
0m0.000s
You can implement missing steps with the snippets below:
#Given("^I open Chrome browser$")
public void i_open_Chrome_browser() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Given("^user open google$")
public void user_open_google() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#When("^user enter powercor in textbox$")
public void user_enter_powercor_in_textbox() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Then("^search results should should poercor\\.com\\.au$")
public void search_results_should_should_poercor_com_au() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Process finished with exit code 1
Execution happens through below :
clean install -DtagArg=#google1
POM.XML is:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>FW_BDD</groupId>
<artifactId>FW_BDD</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<tagArg>~#ignore</tagArg>
<format>pretty</format>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>test/java/.</additionalClasspathElement>
<additionalClasspathElement>src/test/java/stepDefs</additionalClasspathElement>
<additionalClasspathElement>test/java/.</additionalClasspathElement>
</additionalClasspathElements>
<skipTests>true</skipTests>
<!--<includes>
<include>**/*test.java</include>
</includes>-->
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<classpathScope>test</classpathScope>
<mainClass>cucumber.api.cli.Main</mainClass>
<arguments>
<argument>--plugin</argument>
<argument>junit:reports/junit.xml</argument>
<argument>--plugin</argument>
<argument>json:reports/json.json</argument>
<argument>--plugin</argument>
<argument>html:reports/</argument>
<argument>--plugin</argument>
<argument>pretty</argument>
<argument>--strict</argument>
<argument>--glue</argument>
<argument>target/test-classes</argument>
<argument>target/test-classes/.</argument>
<argument>--tags</argument>
<argument>~#ignore</argument>
<argument>--tags</argument>
<argument>${tagArg}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I tried using groovy over Java and things work very well. I use IntelliJ and am not sure what I am missing.
The "--glue" argument is incorrect. This should be the full package name for the step definition classes instead of "target/test-classes". For example, if those classes are located in the "com.cucumber.steps" package, then the POM should look like:
<argument>--glue</argument>
<argument>com.cucumber.steps</argument>
...
<argument>src/test/resources/features</argument>

maven custom lifecycle is not recognized

I am trying to test a simple custom plugin with custom lifecycle.
Here is my EchoMojo.java file:
package org.sonatype.mavenbook.plugins;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
/**
* Echos an object string to the output screen.
* #goal echo
* #execute lifecycle="custom-echo" phase="package"
*/
public class EchoMojo extends AbstractMojo
{
/**
* Any Object to print out.
* #parameter expression="${echo.message}" default-value="Hello World..."
*/
private Object message;
public void execute()
throws MojoExecutionException, MojoFailureException
{
getLog().info( message.toString() );
}
}
My pom.xml looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven- v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.plugins</groupId>
<artifactId>echotest-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0-SNAPSHOT</version>
<name>echotest-plugin Maven Mojo</name>
<url>http://maven.apache.org</url>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I added this in my settings.xml:
<pluginGroups>
<pluginGroup>org.sonatype.mavenbook.plugins</pluginGroup>
</pluginGroups>
I created META-INF/maven/lifecycle.xml
<lifecycles>
<lifecycle>
<id>custom-echo</id>
<phases>
<phase>
<id>package</id>
<executions>
<execution>
<goals>
<goal>echo</goal>
</goals>
</execution>
</executions>
</phase>
</phases>
</lifecycle>
</lifecycles>
I have created META-INF/plexus/components.xml
<component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>custom-echo</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
<configuration>
<phases>
<package>org.sonatype.mavenbook.plugins:echotest-maven-plugin:echo</package>
</phases>
</configuration>
</component>
</components>
</component-set>
When i do mvn echotest:echo, I get "Build Sucessfull".
When i do mvn custom-echo, I get "[ERROR] Unknown lifecycle phase "custom-echo"."
All i wanted is to active my custom plugin when i execute "mvn custom-echo", where custom-echo is a custom lifecycle name
Can someone help me out please.

maven jdepend fail build with cycles

Is there way to use the jdepend plugin in maven to fail a build when package cycles exist? I know you can do it fairly easily with ant, but I haven't figured out how to do it with maven.
thanks,
Jeff
Based on the accepted answer, I improved the performance and log output and released it on Maven Central:
https://github.com/andrena/no-package-cycles-enforcer-rule
(I'd comment on the accepted answer, but don't have enough rep yet.)
You could write your own rule for the maven-enforcer plugin as described in
http://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html
That's how I did it.
NoPackageCyclesRule.java
package org.apache.maven.enforcer.rule;
import java.io.File;
import java.io.IOException;
import jdepend.framework.JDepend;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
public class NoPackageCyclesRule implements EnforcerRule
{
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException
{
Log log = helper.getLog();
try
{
MavenProject project = (MavenProject) helper.evaluate("${project}");
File targetDir = new File((String) helper.evaluate("${project.build.directory}"));
File classesDir = new File(targetDir, "classes");
if (project.getPackaging().equalsIgnoreCase("jar") && classesDir.exists())
{
JDepend jdepend = new JDepend();
jdepend.addDirectory(classesDir.getAbsolutePath());
jdepend.analyze();
if (jdepend.containsCycles())
{
throw new EnforcerRuleException("There are package cycles");
}
}
else
{
log.warn("Skipping jdepend analysis as " + classesDir + " does not exist.");
}
}
catch (ExpressionEvaluationException e)
{
throw new EnforcerRuleException("Unable to lookup an expression "
+ e.getLocalizedMessage(), e);
}
catch (IOException e)
{
throw new EnforcerRuleException("Unable to access target directory "
+ e.getLocalizedMessage(), e);
}
}
public String getCacheId()
{
return "";
}
public boolean isCacheable()
{
return false;
}
public boolean isResultValid(EnforcerRule arg0)
{
return false;
}
}
pom.xml for enforcer rule:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>custom-rule</groupId>
<artifactId>no-package-cycles-rule</artifactId>
<version>1.0</version>
<properties>
<api.version>1.0</api.version>
<maven.version>2.2.1</maven.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven.enforcer</groupId>
<artifactId>enforcer-api</artifactId>
<version>${api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>jdepend</groupId>
<artifactId>jdepend</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
</project>
Then you can add it to your build:
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<dependencies>
<dependency>
<groupId>custom-rule</groupId>
<artifactId>no-package-cycles-rule</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>enforce-no-package-cycles</id>
<goals>
<goal>enforce</goal>
</goals>
<phase>verify</phase> <!-- use a phase after compile! -->
<configuration>
<rules>
<NoPackageCyclesRule
implementation="org.apache.maven.enforcer.rule.NoPackageCyclesRule" />
</rules>
</configuration>
</execution>
</executions>
</plugin>
From what I can see, the JDepend Maven Plugin is supposed to be used to generate a report, it doesn't allow to fail the build on particular rules violations.

Upload/Download entire directory to Nexus through Maven

Is it possible to upload/download an entire directory and all of the sub-directories within it to/from a Nexus repository server?
In case you want to actually deploy a hierarchy of files, I hacked together a solution using GMaven (groovy embedded in maven).
Use the pom below, supply a few properties and hit mvn install.
The folder will be crawled and all files inside it will be deployed using an artifactId taken from the relative path. e.g.
Given the base folder
c:\a\b\c
the file
c:\a\b\c\def\ghi\jkl.mno
would have the artifactId def-ghi-jkl and the packaging mno, this can of course be changed to something else.
The repository info will be taken from the pom, so you need to supply a distributionManagement element in the pom.
Here it is (a lot of this code is taken from the deploy:deploy-file mojo):
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>folderdeployer</artifactId>
<packaging>jar</packaging>
<version>SNAPSHOT</version>
<properties>
<!-- This is where your artifacts are -->
<deploy.basefolder>c:\temp\stuff</deploy.basefolder>
<!-- This will be used as groupId -->
<deploy.groupId>my.groupid</deploy.groupId>
<!-- this will be used as version -->
<deploy.version>1.2.3</deploy.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>deploy-files</id>
<phase>prepare-package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
<![CDATA[
// read components from plexus container
def layout = session.lookup(
'org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout');
def repoFactory = session.lookup(
'org.apache.maven.artifact.repository.ArtifactRepositoryFactory');
def repository = repoFactory.createDeploymentArtifactRepository(
pom.distributionManagement.repository.id,
pom.distributionManagement.repository.url,
layout, true );
def localRepository = session.localRepository;
def helper =
session.lookup("org.apache.maven.project.MavenProjectHelper");
def afm = session.lookup(
'org.apache.maven.artifact.handler.manager.ArtifactHandlerManager');
def factory = new org.apache.maven.artifact.factory.DefaultArtifactFactory();
factory.class.getDeclaredField("artifactHandlerManager").accessible = true;
factory.artifactHandlerManager=afm;
def deployer = session.lookup(
'org.apache.maven.artifact.deployer.ArtifactDeployer');
// initialize properties
def baseFolder = new File(pom.properties['deploy.basefolder']);
def groupId = pom.properties['deploy.groupId'];
def version = pom.properties['deploy.version'];
// iterate over all files recursively
baseFolder.eachFileRecurse{
if(it.isDirectory())return;
// packaging = file.extension
def packaging = it.name.replaceAll( /.+\./ , '' );
// artifactId = file.relativePath.replace '/' , '-'
def artifactId = it.absolutePath
.replace(baseFolder.absolutePath, '')
.substring(1)
.replaceFirst( /\..*?$/ , '')
.replaceAll( /\W+/ , '-' );
def artifact =
factory.createBuildArtifact(
groupId, artifactId, version, packaging );
// create pom for artifact
def model = new org.apache.maven.model.Model();
model.setModelVersion( "4.0.0" );
model.setGroupId( groupId );
model.setArtifactId( artifactId );
model.setVersion( version );
model.setPackaging( packaging );
File pomFile = File.createTempFile( "mvndeploy", ".pom" );
pomFile.deleteOnExit();
fw = org.codehaus.plexus.util.WriterFactory.newXmlWriter( pomFile );
new org.apache.maven.model.io.xpp3.MavenXpp3Writer().write( fw, model );
org.apache.commons.io.IOUtils.closeQuietly( fw );
def metadata =
new org.apache.maven.project.artifact.ProjectArtifactMetadata(
artifact, pomFile );
artifact.addMetadata( metadata );
// deploy file
deployer.deploy(it, artifact, repository, localRepository );
}
]]>
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>your repo id here</id>
<url>scp://your.repo.url/here</url>
<layout>default</layout>
</repository>
</distributionManagement>
</project>
EDIT:
I elaborated on this on my blog
You can always zip the directory and ship it as a zip file.
the users of this folder can download it from Nexus and unzip using dependency:unpack.