September 18, 2009

How to resolve an artifact with maven embedder

« runtime replacement of jar file | Main | PAX Runner - OSGI Provisioning (1) »

Maven Embedder allows you to execute maven commands within  your application at runtime.

This is an example of resolving an artifact ...

 ...

 9 import java.io.File; 
10 import java.util.ArrayList; 
11 import java.util.List; 
12 import org.apache.log4j.Logger; 
13 import org.apache.maven.artifact.Artifact; 
14 import org.apache.maven.artifact.repository.ArtifactRepository; 
15 import org.apache.maven.artifact.repository.DefaultArtifactRepository; 
16 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; 
17 import org.apache.maven.artifact.resolver.ArtifactNotFoundException; 
18 import org.apache.maven.artifact.resolver.ArtifactResolutionException; 
19 import org.apache.maven.embedder.Configuration; 
20 import org.apache.maven.embedder.ConfigurationValidationResult; 
21 import org.apache.maven.embedder.DefaultConfiguration; 
22 import org.apache.maven.embedder.MavenEmbedder; 
23 import org.apache.maven.embedder.MavenEmbedderException; 
24 import org.apache.maven.model.Profile; 
25 import org.apache.maven.model.Repository; 
26 import org.apache.maven.settings.SettingsUtils; 
...
 77     /** 
 78       * resolve artifact. 
 79       * @param groupId group id of artifact 
 80       * @param artifactId artifact id of artifact 
 81       * @param version version of artifact 
 82       * @return downloaded artifact file 
 83       * @throws HostingOrderException error occured during resolution 
 84       */ 
 85      public File resolveArtifact(String groupId, String artifactId, 
 86              String version) throws Exception { 
 87  
 88          LOG.debug("request to resolve '" + groupId + ":" 
 89                  + artifactId + ":" + version + "'"); 
 90   
 91          Artifact artifact = null; 
 92  
 93          LOG.debug("using settings: " + this.settingsFile); 
 94  
 95          File settings = new File(this.getClass().getClassLoader() 
 96                  .getResource(this.settingsFile).getFile()); 
 97  
 98         Configuration configuration = new DefaultConfiguration() 
 99             .setGlobalSettingsFile(SETTINGS) 
100             .setClassLoader(this.classLoader); 
101  
102  
103         ConfigurationValidationResult validationResult = 
104                 MavenEmbedder.validateConfiguration(configuration); 
105  
106         if (validationResult.isValid()) { 
107  
108             try { 
109  
110                 MavenEmbedder embedder = new MavenEmbedder(configuration); 
111  
112                 artifact = embedder.createArtifact(groupId, 
113                         artifactId, version, null, "jar"); 
114  
115                 // assign repos, 
116                 List repos = new ArrayList(); 
117                 Profile profile = SettingsUtils.convertFromSettingsProfile( 
118                         (org.apache.maven.settings.Profile) 
119                          embedder.getSettings().getProfiles().get(0)); 
120                 for (Repository r : (List < Repository > ) profile. 
121                         getRepositories()) { 
122                       ArtifactRepository repo = new DefaultArtifactRepository( 
123                             r.getId(), 
124                             r.getUrl(), 
125                             new DefaultRepositoryLayout()); 
126                       repos.add(repo); 
127                     LOG.debug("added repo " + r.getId() + ":" 
128                             + r.getUrl()); 
129                 } 
130  
131                 embedder.resolve(artifact, repos, 
132                         embedder.getLocalRepository()); 
133  
134             } catch (MavenEmbedderException mee) {
   ...
137             } catch (ArtifactResolutionException are) { 
   ...
140             } catch (ArtifactNotFoundException ane) { 
   ... 
143             } finally {
144                 configuration = null; 
145                 validationResult = null; 
146             } 
147  
148             LOG.info(artifact.getFile().getPath()); 
149  
150             return artifact.getFile(); 
151  
152  
153         } else { 
154             LOG.error("settings file did not validate !!"); 
155  
156             if (!validationResult.isUserSettingsFilePresent()) { 
157                 LOG.warn("The specific user settings file "'  + settings + "' is not present.);
... 
159             } else if (!validationResult.isUserSettingsFileParses()) { 
160                 LOG.warn("Please check your settings file, it is not well formed XML.
...
162             } 
163         } 
164  
165         return null; 
166  
167     } 
 

Maven repositories are defined in a usual way, in a SETTINGS file (see line 99).

Important is to lookout for setting the correct classloader (line 100), especially in a servlet context.

Everything else is just straight forward ...

In combination with  runtime replacement of jar, this can be a very powerful solution ...

Posted by peter.grund at 7:51 AM in Maven

 

[Trackback URL for this entry]

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« September »
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
27282930