blob: 24981ca209da2145d46dc6d140e2eeba03faa89b [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.bnd.maven;
2
3import java.io.*;
4import java.util.*;
5import java.util.jar.*;
6
7import aQute.bnd.build.*;
8import aQute.bnd.service.*;
9import aQute.lib.osgi.*;
10import aQute.libg.command.*;
11import aQute.libg.reporter.*;
12
13public class MavenDeploy implements Deploy, Plugin {
14
15 String repository;
16 String url;
17 String homedir;
18 String keyname;
19
20 String passphrase;
21 Reporter reporter;
22
23 public void setProperties(Map<String, String> map) {
24 repository = map.get("repository");
25 url = map.get("url");
26 passphrase = map.get("passphrase");
27 homedir = map.get("homedir");
28 keyname = map.get("keyname");
29
30 if (url == null)
31 throw new IllegalArgumentException("MavenDeploy plugin must get a repository URL");
32 if (repository == null)
33 throw new IllegalArgumentException("MavenDeploy plugin must get a repository name");
34 }
35
36 public void setReporter(Reporter processor) {
37 this.reporter = processor;
38 }
39
40 /**
41 */
42 public boolean deploy(Project project, Jar original) throws Exception {
43 Map<String, Map<String, String>> deploy = project.parseHeader(project
44 .getProperty(Constants.DEPLOY));
45
46 Map<String, String> maven = deploy.get(repository);
47 if (maven == null)
48 return false; // we're not playing for this bundle
49
50 project.progress("deploying %s to Maven repo: %s", original, repository);
51 File target = project.getTarget();
52 File tmp = Processor.getFile(target, repository);
53 tmp.mkdirs();
54
55 Manifest manifest = original.getManifest();
56 if (manifest == null)
57 project.error("Jar has no manifest: %s", original);
58 else {
59 project.progress("Writing pom.xml");
60 PomResource pom = new PomResource(manifest);
61 pom.setProperties(maven);
62 File pomFile = write(tmp, pom, "pom.xml");
63
64 Jar main = new Jar("main");
65 Jar src = new Jar("src");
66 try {
67 split(original, main, src);
68 Map<String, Map<String, String>> exports = project.parseHeader(manifest
69 .getMainAttributes().getValue(Constants.EXPORT_PACKAGE));
70 File jdoc = new File(tmp, "jdoc");
71 jdoc.mkdirs();
72 project.progress("Generating Javadoc for: " + exports.keySet());
73 Jar javadoc = javadoc(jdoc, project, exports.keySet());
74 project.progress("Writing javadoc jar");
75 File javadocFile = write(tmp, new JarResource(javadoc), "javadoc.jar");
76 project.progress("Writing main file");
77 File mainFile = write(tmp, new JarResource(main), "main.jar");
78 project.progress("Writing sources file");
79 File srcFile = write(tmp, new JarResource(main), "src.jar");
80
81 project.progress("Deploying main file");
82 maven_gpg_sign_and_deploy(project, mainFile, null, pomFile);
83 project.progress("Deploying main sources file");
84 maven_gpg_sign_and_deploy(project, srcFile, "sources", null);
85 project.progress("Deploying main javadoc file");
86 maven_gpg_sign_and_deploy(project, javadocFile, "javadoc", null);
87
88 } finally {
89 main.close();
90 src.close();
91 }
92 }
93 return true;
94 }
95
96 private void split(Jar original, Jar main, Jar src) {
97 for (Map.Entry<String, Resource> e : original.getResources().entrySet()) {
98 String path = e.getKey();
99 if (path.startsWith("OSGI-OPT/src/")) {
100 src.putResource(path.substring("OSGI-OPT/src/".length()), e.getValue());
101 } else {
102 main.putResource(path, e.getValue());
103 }
104 }
105 }
106
107 // gpg:sign-and-deploy-file \
108 // -Durl=http://oss.sonatype.org/service/local/staging/deploy/maven2
109 // \
110 // -DrepositoryId=sonatype-nexus-staging \
111 // -DupdateReleaseInfo=true \
112 // -DpomFile=pom.xml \
113 // -Dfile=/Ws/bnd/biz.aQute.bndlib/tmp/biz.aQute.bndlib.jar \
114 // -Dpassphrase=a1k3v3t5x3
115
116 private void maven_gpg_sign_and_deploy(Project b, File file, String classifier, File pomFile)
117 throws Exception {
118 Command command = new Command();
119 command.setTrace();
120 command.add(b.getProperty("mvn", "mvn"));
121 command.add("gpg:sign-and-deploy-file", "-DreleaseInfo=true", "-DpomFile=pom.xml");
122 command.add("-Dfile=" + file.getAbsolutePath());
123 command.add("-DrepositoryId=" + repository);
124 command.add("-Durl=" + url);
125 optional(command, "passphrase", passphrase);
126 optional(command, "keyname", keyname);
127 optional(command, "homedir", homedir);
128 optional(command, "classifier", classifier);
129 optional(command, "pomFile", pomFile == null ? null : pomFile.getAbsolutePath());
130
131 StringBuffer stdout = new StringBuffer();
132 StringBuffer stderr = new StringBuffer();
133
134 int result = command.execute(stdout, stderr);
135 if (result != 0) {
136 b.error("Maven deploy to %s failed to sign and transfer %s because %s", repository,
137 file, "" + stdout + stderr);
138 }
139 }
140
141 private void optional(Command command, String key, String value) {
142 if (value == null)
143 return;
144
145 command.add("-D=" + value);
146 }
147
148 private Jar javadoc(File tmp, Project b, Set<String> exports) throws Exception {
149 Command command = new Command();
150
151 command.add(b.getProperty("javadoc", "javadoc"));
152 command.add("-d");
153 command.add(tmp.getAbsolutePath());
154 command.add("-sourcepath");
155 command.add( Processor.join(b.getSourcePath(),File.pathSeparator));
156
157 for (String packageName : exports) {
158 command.add(packageName);
159 }
160
161 StringBuffer out = new StringBuffer();
162 StringBuffer err = new StringBuffer();
163 Command c = new Command();
164 c.setTrace();
165 int result = c.execute(out, err);
166 if (result == 0) {
167 Jar jar = new Jar(tmp);
168 b.addClose(jar);
169 return jar;
170 }
171 b.error("Error during execution of javadoc command: %s / %s", out, err);
172 return null;
173 }
174
175 private File write(File base, Resource r, String fileName) throws Exception {
176 File f = Processor.getFile(base, fileName);
177 OutputStream out = new FileOutputStream(f);
178 try {
179 r.write(out);
180 } finally {
181 out.close();
182 }
183 return f;
184 }
185
186}