blob: 8d1909377b5411c63f1893e619b20718d2eb94be [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.maven;
2
3import java.io.*;
4import java.util.*;
5import java.util.jar.*;
6
7import aQute.bnd.build.*;
Stuart McCulloch42151ee2012-07-16 13:43:38 +00008import aQute.bnd.header.*;
9import aQute.bnd.osgi.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000010import aQute.libg.command.*;
Stuart McCulloch1a890552012-06-29 19:23:09 +000011import aQute.service.reporter.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000012
13public class MavenDeployCmd extends Processor {
14
15 String repository = "nexus";
16 String url = "http://oss.sonatype.org/service/local/staging/deploy/maven2";
17 String homedir;
18 String keyname;
19
20 String passphrase;
21 Reporter reporter;
22
23 /**
24 * maven deploy [-url repo] [-passphrase passphrase] [-homedir homedir]
25 * [-keyname keyname] bundle ...
26 *
27 * @param args
28 * @param i
29 * @throws Exception
30 */
31 void run(String args[], int i) throws Exception {
32 if (i >= args.length) {
Stuart McCulloch54229442012-07-12 22:12:58 +000033 System.err.printf("Usage:%n");
Stuart McCullochf3173222012-06-07 21:57:32 +000034 System.err
Stuart McCulloch4482c702012-06-15 13:27:53 +000035 .println(" deploy [-url repo] [-passphrase passphrase] [-homedir homedir] [-keyname keyname] bundle ...");
Stuart McCullochf3173222012-06-07 21:57:32 +000036 System.err.println(" settings");
37 return;
38 }
39
40 /* skip first argument */
41 i++;
Stuart McCulloch4482c702012-06-15 13:27:53 +000042
Stuart McCullochf3173222012-06-07 21:57:32 +000043 while (i < args.length && args[i].startsWith("-")) {
44 String option = args[i];
45 if (option.equals("-url"))
46 repository = args[++i];
47 else if (option.equals("-passphrase"))
48 passphrase = args[++i];
49 else if (option.equals("-url"))
50 homedir = args[++i];
51 else if (option.equals("-keyname"))
52 keyname = args[++i];
53 else
54 error("Invalid command ");
55 }
56
Stuart McCullochf3173222012-06-07 21:57:32 +000057 }
58
Stuart McCulloch4482c702012-06-15 13:27:53 +000059 public void setProperties(Map<String,String> map) {
Stuart McCullochf3173222012-06-07 21:57:32 +000060 repository = map.get("repository");
61 url = map.get("url");
62 passphrase = map.get("passphrase");
63 homedir = map.get("homedir");
64 keyname = map.get("keyname");
65
66 if (url == null)
67 throw new IllegalArgumentException("MavenDeploy plugin must get a repository URL");
68 if (repository == null)
69 throw new IllegalArgumentException("MavenDeploy plugin must get a repository name");
70 }
71
72 public void setReporter(Reporter processor) {
73 this.reporter = processor;
74 }
75
76 /**
77 */
78 public boolean deploy(Project project, Jar original) throws Exception {
Stuart McCulloch4482c702012-06-15 13:27:53 +000079 Parameters deploy = project.parseHeader(project.getProperty(Constants.DEPLOY));
Stuart McCullochf3173222012-06-07 21:57:32 +000080
Stuart McCulloch4482c702012-06-15 13:27:53 +000081 Map<String,String> maven = deploy.get(repository);
Stuart McCullochf3173222012-06-07 21:57:32 +000082 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);
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000088 if (!tmp.exists() && !tmp.mkdirs()) {
89 throw new IOException("Could not create directory " + tmp);
90 }
Stuart McCullochf3173222012-06-07 21:57:32 +000091
92 Manifest manifest = original.getManifest();
93 if (manifest == null)
94 project.error("Jar has no manifest: %s", original);
95 else {
96 project.progress("Writing pom.xml");
97 PomResource pom = new PomResource(manifest);
98 pom.setProperties(maven);
99 File pomFile = write(tmp, pom, "pom.xml");
100
101 Jar main = new Jar("main");
102 Jar src = new Jar("src");
103 try {
104 split(original, main, src);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000105 Parameters exports = project.parseHeader(manifest.getMainAttributes()
106 .getValue(Constants.EXPORT_PACKAGE));
Stuart McCullochf3173222012-06-07 21:57:32 +0000107 File jdoc = new File(tmp, "jdoc");
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000108 if (!jdoc.exists() && !jdoc.mkdirs()) {
109 throw new IOException("Could not create directory " + jdoc);
110 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000111 project.progress("Generating Javadoc for: " + exports.keySet());
112 Jar javadoc = javadoc(jdoc, project, exports.keySet());
113 project.progress("Writing javadoc jar");
114 File javadocFile = write(tmp, new JarResource(javadoc), "javadoc.jar");
115 project.progress("Writing main file");
116 File mainFile = write(tmp, new JarResource(main), "main.jar");
117 project.progress("Writing sources file");
118 File srcFile = write(tmp, new JarResource(main), "src.jar");
119
120 project.progress("Deploying main file");
121 maven_gpg_sign_and_deploy(project, mainFile, null, pomFile);
122 project.progress("Deploying main sources file");
123 maven_gpg_sign_and_deploy(project, srcFile, "sources", null);
124 project.progress("Deploying main javadoc file");
125 maven_gpg_sign_and_deploy(project, javadocFile, "javadoc", null);
126
Stuart McCulloch4482c702012-06-15 13:27:53 +0000127 }
128 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000129 main.close();
130 src.close();
131 }
132 }
133 return true;
134 }
135
136 private void split(Jar original, Jar main, Jar src) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000137 for (Map.Entry<String,Resource> e : original.getResources().entrySet()) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000138 String path = e.getKey();
139 if (path.startsWith("OSGI-OPT/src/")) {
140 src.putResource(path.substring("OSGI-OPT/src/".length()), e.getValue());
141 } else {
142 main.putResource(path, e.getValue());
143 }
144 }
145 }
146
147 // gpg:sign-and-deploy-file \
148 // -Durl=http://oss.sonatype.org/service/local/staging/deploy/maven2
149 // \
150 // -DrepositoryId=sonatype-nexus-staging \
151 // -DupdateReleaseInfo=true \
152 // -DpomFile=pom.xml \
153 // -Dfile=/Ws/bnd/biz.aQute.bndlib/tmp/biz.aQute.bndlib.jar \
154 // -Dpassphrase=a1k3v3t5x3
155
Stuart McCulloch4482c702012-06-15 13:27:53 +0000156 private void maven_gpg_sign_and_deploy(Project b, File file, String classifier, File pomFile) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +0000157 Command command = new Command();
158 command.setTrace();
159 command.add(b.getProperty("mvn", "mvn"));
160 command.add("gpg:sign-and-deploy-file", "-DreleaseInfo=true", "-DpomFile=pom.xml");
161 command.add("-Dfile=" + file.getAbsolutePath());
162 command.add("-DrepositoryId=" + repository);
163 command.add("-Durl=" + url);
164 optional(command, "passphrase", passphrase);
165 optional(command, "keyname", keyname);
166 optional(command, "homedir", homedir);
167 optional(command, "classifier", classifier);
168 optional(command, "pomFile", pomFile == null ? null : pomFile.getAbsolutePath());
169
170 StringBuilder stdout = new StringBuilder();
171 StringBuilder stderr = new StringBuilder();
172
173 int result = command.execute(stdout, stderr);
174 if (result != 0) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000175 b.error("Maven deploy to %s failed to sign and transfer %s because %s", repository, file, "" + stdout
176 + stderr);
Stuart McCullochf3173222012-06-07 21:57:32 +0000177 }
178 }
179
Stuart McCulloch669423b2012-06-26 16:34:24 +0000180 private void optional(Command command, @SuppressWarnings("unused") String key, String value) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000181 if (value == null)
182 return;
183
184 command.add("-D=" + value);
185 }
186
187 private Jar javadoc(File tmp, Project b, Set<String> exports) throws Exception {
188 Command command = new Command();
189
190 command.add(b.getProperty("javadoc", "javadoc"));
191 command.add("-d");
192 command.add(tmp.getAbsolutePath());
193 command.add("-sourcepath");
194 command.add(Processor.join(b.getSourcePath(), File.pathSeparator));
195
196 for (String packageName : exports) {
197 command.add(packageName);
198 }
199
200 StringBuilder out = new StringBuilder();
201 StringBuilder err = new StringBuilder();
202 Command c = new Command();
203 c.setTrace();
204 int result = c.execute(out, err);
205 if (result == 0) {
206 Jar jar = new Jar(tmp);
207 b.addClose(jar);
208 return jar;
209 }
210 b.error("Error during execution of javadoc command: %s / %s", out, err);
211 return null;
212 }
213
214 private File write(File base, Resource r, String fileName) throws Exception {
215 File f = Processor.getFile(base, fileName);
216 OutputStream out = new FileOutputStream(f);
217 try {
218 r.write(out);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000219 }
220 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000221 out.close();
222 }
223 return f;
224 }
225
226}