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