Changeset 13355


Ignore:
Timestamp:
06/21/11 21:18:51 (10 years ago)
Author:
Mark Evenson
Message:

ASDF-ABCL:RESOLVE returns Maven dependencies from local repository.

I don't think the download portion is quite working yet.

Location:
trunk/abcl/contrib/abcl-asdf
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/contrib/abcl-asdf/abcl-asdf.asd

    r13354 r13355  
    55  :author "Mark Evenson"
    66  :version "0.2.0"
     7  :depends-on ("jss") ;;; XXX move the JSS ASDf defintions here? uggh.
    78  :components
    89  ((:module base :pathname "" :components
  • trunk/abcl/contrib/abcl-asdf/maven-embedder.lisp

    r13354 r13355  
     1;;; Use the Aether system in a default maven distribution to download
     2;;; and install dependencies.
     3;;;
     4;;; https://docs.sonatype.org/display/AETHER/Home
     5;;;
     6
    17(in-package :abcl-asdf)
    28
     
    410(require :jss)
    511
    6 (defvar *mvn-directory*
     12(defparameter *mvn-directory*
    713  "/export/home/evenson/work/apache-maven-3.0.3/lib/"
    814  "Location of 'maven-core-3.<m>.<p>.jar', 'maven-embedder-3.<m>.<p>.jar' etc.")
    915
    1016(defun init ()
     17  (unless (probe-file *mvn-directory*)
     18    (error "You must download Maven 3 from http://maven.apache.org/download.html, then set ABCL-ASDF:*MVN-DIRECTORY* appropiately."))
    1119  (jss:add-directory-jars-to-class-path *mvn-directory* nil))
    1220
    13 (defconstant +null+
    14   (java:make-immediate-object :ref nil))
     21(defun repository-system ()
     22  (let ((locator
     23         (java:jnew "org.apache.maven.repository.internal.DefaultServiceLocator"))
     24        (wagon-class
     25         (java:jclass "org.sonatype.aether.connector.wagon.WagonProvider"))
     26        (wagon-provider
     27         (jss:find-java-class "LightweightHttpWagon"))
     28        (repository-connector-factory-class
     29         (java:jclass "org.sonatype.aether.connector.wagon.WagonRepositoryConnector"))
     30        (wagon-repository-connector-factory-class
     31         (java:jclass "org.sonatype.aether.connector.wagon.WagonRepositoryConnectorFactory"))
     32        (repository-system-class
     33         (java:jclass "org.sonatype.aether.RepositorySystem")))
     34    (#"setService" locator wagon-class wagon-provider)
     35    (#"addService" locator
     36                   repository-connector-factory-class
     37                   wagon-repository-connector-factory-class)
     38    (#"getService" locator repository-system-class)))
     39
     40#|
     41private static RepositorySystem newRepositorySystem()
     42{
     43  DefaultServiceLocator locator = new DefaultServiceLocator();
     44  locator.setServices( WagonProvider.class, new ManualWagonProvider() );
     45  locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
     46
     47  return locator.getService( RepositorySystem.class );
     48}
     49|#
     50
     51(defun new-session (repository-system)
     52  (let ((session
     53         (java:jnew (jss:find-java-class "MavenRepositorySystemSession")))
     54        (local-repository
     55         (java:jnew (jss:find-java-class "LocalRepository")
     56                  (namestring (merge-pathnames ".m2/repository/"
     57                                               (user-homedir-pathname))))))
     58    (#"setLocalRepositoryManager"
     59     session
     60     (#"newLocalRepositoryManager" repository-system local-repository))))
     61
     62#|
     63private static RepositorySystemSession newSession( RepositorySystem system )
     64{
     65  MavenRepositorySystemSession session = new MavenRepositorySystemSession();
     66
     67  LocalRepository localRepo = new LocalRepository( "target/local-repo" );
     68  session.setLocalRepositoryManager( system.newLocalRepositoryManager( localRepo ) );
     69 
     70  return session;
     71}
     72|#
     73
     74;;; XXX make-immediate-object is deprecated
     75(defconstant +null+ (java:make-immediate-object nil :ref))
    1576
    1677(defun resolve (group-id artifact-id version)
    17   (let* ((configuration (find-configuration))
    18          (embedder (jss:new 'MavenEmbedder configuration))
    19          (artifact (#"create" embedder group-id version +null+ "jar")))
    20     (pathname (#"toString" (#"getFile" artifact)))))
    21 
    22 (defun find-configuration ())
    23 
    24 
    25 
     78  (let* ((system
     79          (repository-system))
     80         (session
     81          (new-session system))
     82         (artifact
     83          (java:jnew (jss:find-java-class "aether.util.artifact.DefaultArtifact")
     84                     (format nil "~A:~A:~A"
     85                             group-id artifact-id version)))
     86         (dependency
     87          (java:jnew (jss:find-java-class "aether.graph.Dependency")
     88                     artifact "compile"))
     89         (central
     90          (java:jnew (jss:find-java-class "RemoteRepository")
     91                     "central" "default"
     92                     "http://repo1.maven.org/maven2/"))
     93         (collect-request (java:jnew (jss:find-java-class "CollectRequest"))))
     94    (#"setRoot" collect-request dependency)
     95    (#"addRepository" collect-request central)
     96    (let* ((node
     97            (#"getRoot" (#"collectDependencies" system session collect-request)))
     98           (dependency-request
     99            (java:jnew (jss:find-java-class "DependencyRequest")
     100                       node +null+))
     101           (nlg
     102            (java:jnew (jss:find-java-class "PreorderNodeListGenerator"))))
     103      (#"resolveDependencies" system session dependency-request)
     104      (#"accept" node nlg)
     105      (#"getClassPath" nlg))))
    26106
    27107#|
    28 // http://developers-blog.org/blog/default/2009/09/18/How-to-resolve-an-artifact-with-maven-embedder
     108public static void main( String[] args )
     109  throws Exception
     110{
     111  RepositorySystem repoSystem = newRepositorySystem();
    29112
    30 import java.util.ArrayList;
     113  RepositorySystemSession session = newSession( repoSystem );
    31114
    32 import java.util.List;
    33 import org.apache.log4j.Logger;
    34 import org.apache.maven.artifact.Artifact;
    35 import org.apache.maven.artifact.repository.ArtifactRepository;
    36 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
    37 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
    38 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
    39 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
    40 import org.apache.maven.embedder.Configuration;
    41 import org.apache.maven.embedder.ConfigurationValidationResult;
    42 import org.apache.maven.embedder.DefaultConfiguration;
    43 import org.apache.maven.embedder.MavenEmbedder;
    44 import org.apache.maven.embedder.MavenEmbedderException;
    45 import org.apache.maven.model.Profile;
    46 import org.apache.maven.model.Repository;
    47 import org.apache.maven.settings.SettingsUtils;
     115  Dependency dependency =
     116    new Dependency( new DefaultArtifact( "org.apache.maven:maven-profile:2.2.1" ), "compile" );
     117  RemoteRepository central = new RemoteRepository( "central", "default", "http://repo1.maven.org/maven2/" );
    48118
     119  CollectRequest collectRequest = new CollectRequest();
     120  collectRequest.setRoot( dependency );
     121  collectRequest.addRepository( central );
     122  DependencyNode node = repoSystem.collectDependencies( session, collectRequest ).getRoot();
    49123
    50 /**
    51  * resolve artifact.
    52  * @param groupId group id of artifact
    53  * @param artifactId artifact id of artifact
    54  * @param version version of artifact
    55  * @return downloaded artifact file
    56  * @throws HostingOrderException error occured during resolution
    57  */
     124  DependencyRequest dependencyRequest = new DependencyRequest( node, null );
    58125
    59 public File resolveArtifact(String groupId, String artifactId, String version)
    60   throws Exception
    61 {
    62   LOG.debug("request to resolve '" + groupId + ":"
    63             + artifactId + ":" + version + "'");
    64   Artifact artifact = null;
    65   LOG.debug("using settings: " + this.settingsFile);
    66   File settings = new File(this.getClass().getClassLoader()
    67                            .getResource(this.settingsFile).getFile());
    68   Configuration configuration = new DefaultConfiguration()
    69     .setGlobalSettingsFile(SETTINGS)
    70     .setClassLoader(this.classLoader);
    71   ConfigurationValidationResult validationResult =
    72     MavenEmbedder.validateConfiguration(configuration);
    73   if (validationResult.isValid()) {
    74     try {
    75       MavenEmbedder embedder = new MavenEmbedder(configuration);
    76       artifact = embedder.createArtifact(groupId,
    77                                          artifactId, version, null, "jar");
    78       // assign repos,
    79       List repos = new ArrayList();
    80       Profile profile = SettingsUtils.convertFromSettingsProfile((org.apache.maven.settings.Profile)
    81                                                                  embedder.getSettings().getProfiles().get(0));
    82       for (Repository r : (List < Repository > ) profile.
    83              getRepositories()) {
    84         ArtifactRepository repo = new DefaultArtifactRepository(r.getId(),
    85                                                                 r.getUrl(),
    86                                                                 new DefaultRepositoryLayout());
    87         repos.add(repo);
    88         LOG.debug("added repo " + r.getId() + ":"
    89                   + r.getUrl());
    90       }
    91       embedder.resolve(artifact, repos,
    92                        embedder.getLocalRepository());
    93     } catch (MavenEmbedderException mee) {
    94     } catch (ArtifactResolutionException are) {
    95     } catch (ArtifactNotFoundException ane) {
    96     } finally {
    97       configuration = null;
    98       validationResult = null;
    99     }
    100     LOG.info(artifact.getFile().getPath());
    101     return artifact.getFile();
    102   } else {
    103     LOG.error("settings file did not validate !!");
    104     if (!validationResult.isUserSettingsFilePresent()) {
    105       LOG.warn("The specific user settings file "'  + settings + "' is not present.);
    106     } else if (!validationResult.isUserSettingsFileParses()) {
    107       LOG.warn("Please check your settings file, it is not well formed XML.");
    108     }
    109   }
    110   return null;
    111 }
     126  repoSystem.resolveDependencies( session, dependencyRequest  );
     127
     128  PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
     129  node.accept( nlg );
     130  System.out.println( nlg.getClassPath() );
     131}
    112132|#
     133
     134#|
     135
     136Test:
     137
     138(init)
     139(resolve "org.slf4j" "slf4j-api" "1.6.1")
     140|#
Note: See TracChangeset for help on using the changeset viewer.