This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
o Added a sample project to demonstrate usage of Maven 3.x model buil…
…der with a custom model resolver git-svn-id: file:///opt/svn/repositories/sonatype.org/nexus/trunk@5153 2aa8b3fc-8ebb-4439-a84f-95066eaea8ab
- Loading branch information
Showing
7 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>demo</groupId> | ||
<artifactId>model-builder-demo</artifactId> | ||
<version>0.1</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-model-builder</artifactId> | ||
<version>3.0-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.plexus</groupId> | ||
<artifactId>plexus-container-default</artifactId> | ||
<version>1.2.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.0.2</version> | ||
<configuration> | ||
<source>1.5</source> | ||
<target>1.5</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.1.1</version> | ||
<executions> | ||
<execution> | ||
<id>test</id> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>exec</goal> | ||
</goals> | ||
<configuration> | ||
<executable>java</executable> | ||
<arguments> | ||
<argument>-classpath</argument> | ||
<classpath/> | ||
<argument>org.ModelBuilderDemo</argument> | ||
</arguments> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
21 changes: 21 additions & 0 deletions
21
sandbox/model-builder-demo/src/main/java/org/EmptyLifecycleBindingsInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org; | ||
|
||
import org.apache.maven.model.building.ModelBuildingRequest; | ||
import org.apache.maven.model.building.ModelProblemCollector; | ||
import org.apache.maven.model.plugin.LifecycleBindingsInjector; | ||
import org.apache.maven.model.Model; | ||
|
||
/* | ||
* This is an optional component and only required if the model request has processPlugins=true which is not of interest | ||
* for dependency related tasks. I'm looking into teaching Plexus optional requirements to get rid of this empty class. | ||
*/ | ||
public class EmptyLifecycleBindingsInjector | ||
implements LifecycleBindingsInjector | ||
{ | ||
|
||
public void injectLifecycleBindings( Model model, ModelBuildingRequest request, ModelProblemCollector problems ) | ||
{ | ||
// no-op | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
sandbox/model-builder-demo/src/main/java/org/ModelBuilderDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org; | ||
|
||
import java.io.StringWriter; | ||
|
||
import org.apache.maven.model.Model; | ||
import org.apache.maven.model.building.DefaultModelBuildingRequest; | ||
import org.apache.maven.model.building.ModelBuilder; | ||
import org.apache.maven.model.building.ModelBuildingRequest; | ||
import org.apache.maven.model.building.ModelBuildingResult; | ||
import org.apache.maven.model.building.ModelSource; | ||
import org.apache.maven.model.building.UrlModelSource; | ||
import org.apache.maven.model.io.ModelWriter; | ||
import org.codehaus.plexus.DefaultPlexusContainer; | ||
import org.codehaus.plexus.PlexusContainer; | ||
|
||
public class ModelBuilderDemo | ||
{ | ||
|
||
public static void main( String[] args ) | ||
throws Exception | ||
{ | ||
/* | ||
* setup the container and get us some interesting components | ||
*/ | ||
|
||
PlexusContainer container = new DefaultPlexusContainer(); | ||
|
||
ModelBuilder modelBuilder = container.lookup( ModelBuilder.class ); | ||
|
||
ModelWriter modelWriter = container.lookup( ModelWriter.class ); | ||
|
||
/* | ||
* build some effective model | ||
*/ | ||
|
||
ModelSource modelSource = | ||
new UrlModelSource( ModelBuilderDemo.class.getResource( "/repo/demo/dependency/1.0/pom.xml" ) ); | ||
|
||
ModelBuildingRequest request = new DefaultModelBuildingRequest(); | ||
|
||
request.setModelSource( modelSource ); | ||
request.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL ); | ||
request.setSystemProperties( System.getProperties() ); | ||
request.setModelResolver( new SimpleModelResolver() ); | ||
|
||
ModelBuildingResult result = modelBuilder.build( request ); | ||
|
||
Model effectivePom = result.getEffectiveModel(); | ||
|
||
StringWriter writer = new StringWriter(); | ||
modelWriter.write( writer, null, effectivePom ); | ||
System.out.println( writer.toString() ); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
sandbox/model-builder-demo/src/main/java/org/SimpleModelResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org; | ||
|
||
import java.net.URL; | ||
|
||
import org.apache.maven.model.Repository; | ||
import org.apache.maven.model.building.ModelSource; | ||
import org.apache.maven.model.building.UrlModelSource; | ||
import org.apache.maven.model.resolution.InvalidRepositoryException; | ||
import org.apache.maven.model.resolution.ModelResolver; | ||
import org.apache.maven.model.resolution.UnresolvableModelException; | ||
|
||
public class SimpleModelResolver | ||
implements ModelResolver | ||
{ | ||
|
||
public void addRepository( Repository repository ) | ||
throws InvalidRepositoryException | ||
{ | ||
// no-op, we don't care about POM repos right now | ||
} | ||
|
||
public ModelResolver newCopy() | ||
{ | ||
// this class is stateless so can be reused | ||
return this; | ||
} | ||
|
||
public ModelSource resolveModel( String groupId, String artifactId, String version ) | ||
throws UnresolvableModelException | ||
{ | ||
// do your repo magic here | ||
|
||
String resource = "/repo/" + groupId + '/' + artifactId + '/' + version + "/pom.xml"; | ||
|
||
URL url = getClass().getResource( resource ); | ||
|
||
if ( url == null ) | ||
{ | ||
throw new UnresolvableModelException( "PANIC!", groupId, artifactId, version ); | ||
} | ||
|
||
return new UrlModelSource( url ); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
sandbox/model-builder-demo/src/main/resources/META-INF/plexus/components.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<component-set> | ||
<components> | ||
<component> | ||
<role>org.apache.maven.model.plugin.LifecycleBindingsInjector</role> | ||
<role-hint>default</role-hint> | ||
<implementation>org.EmptyLifecycleBindingsInjector</implementation> | ||
</component> | ||
</components> | ||
</component-set> |
20 changes: 20 additions & 0 deletions
20
sandbox/model-builder-demo/src/main/resources/repo/demo/dependency/1.0/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>demo</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>2</version> | ||
</parent> | ||
|
||
<groupId>demo</groupId> | ||
<artifactId>dependency</artifactId> | ||
<version>1.0</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>demo</groupId> | ||
<artifactId>managed</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
30 changes: 30 additions & 0 deletions
30
sandbox/model-builder-demo/src/main/resources/repo/demo/parent/2/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>demo</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>2</version> | ||
<packaging>pom</packaging> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>demo</groupId> | ||
<artifactId>managed</artifactId> | ||
<version>${interpolated}</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<profiles> | ||
<profile> | ||
<id>demo</id> | ||
<activation> | ||
<jdk>[1.4,)</jdk> | ||
</activation> | ||
<properties> | ||
<interpolated>3.8.2</interpolated> | ||
</properties> | ||
</profile> | ||
</profiles> | ||
</project> |