source: trunk/abcl/contrib/abcl-asdf/maven-embedder.lisp @ 13355

Last change on this file since 13355 was 13355, checked in by Mark Evenson, 10 years ago

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

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

File size: 5.0 KB
Line 
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
7(in-package :abcl-asdf)
8
9(require :abcl-contrib)
10(require :jss)
11
12(defparameter *mvn-directory*
13  "/export/home/evenson/work/apache-maven-3.0.3/lib/"
14  "Location of 'maven-core-3.<m>.<p>.jar', 'maven-embedder-3.<m>.<p>.jar' etc.")
15
16(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."))
19  (jss:add-directory-jars-to-class-path *mvn-directory* nil))
20
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))
76
77(defun resolve (group-id artifact-id version)
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))))
106
107#|
108public static void main( String[] args )
109  throws Exception
110{
111  RepositorySystem repoSystem = newRepositorySystem();
112
113  RepositorySystemSession session = newSession( repoSystem );
114
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/" );
118
119  CollectRequest collectRequest = new CollectRequest();
120  collectRequest.setRoot( dependency );
121  collectRequest.addRepository( central );
122  DependencyNode node = repoSystem.collectDependencies( session, collectRequest ).getRoot();
123
124  DependencyRequest dependencyRequest = new DependencyRequest( node, null );
125
126  repoSystem.resolveDependencies( session, dependencyRequest  );
127
128  PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
129  node.accept( nlg );
130  System.out.println( nlg.getClassPath() );
131}
132|#
133
134#|
135
136Test:
137
138(init)
139(resolve "org.slf4j" "slf4j-api" "1.6.1")
140|#
Note: See TracBrowser for help on using the repository browser.