blob: b7a1c1212639778541510a958f15f038c94b1077 [file] [log] [blame]
Stuart McCullochbb014372012-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.*;
8import aQute.bnd.service.*;
9import aQute.lib.osgi.*;
10import aQute.libg.command.*;
11import aQute.libg.header.*;
12import aQute.libg.reporter.*;
13
14public class MavenDeploy implements Deploy, Plugin {
15
16 String repository;
17 String url;
18 String homedir;
19 String keyname;
20
21 String passphrase;
22 Reporter reporter;
23
Stuart McCulloch2286f232012-06-15 13:27:53 +000024 public void setProperties(Map<String,String> map) {
Stuart McCullochbb014372012-06-07 21:57:32 +000025 repository = map.get("repository");
26 url = map.get("url");
27 passphrase = map.get("passphrase");
28 homedir = map.get("homedir");
29 keyname = map.get("keyname");
30
31 if (url == null)
32 throw new IllegalArgumentException("MavenDeploy plugin must get a repository URL");
33 if (repository == null)
34 throw new IllegalArgumentException("MavenDeploy plugin must get a repository name");
35 }
36
37 public void setReporter(Reporter processor) {
38 this.reporter = processor;
39 }
40
41 /**
42 */
43 public boolean deploy(Project project, Jar original) throws Exception {
Stuart McCulloch2286f232012-06-15 13:27:53 +000044 Parameters deploy = project.parseHeader(project.getProperty(Constants.DEPLOY));
Stuart McCullochbb014372012-06-07 21:57:32 +000045
Stuart McCulloch2286f232012-06-15 13:27:53 +000046 Map<String,String> maven = deploy.get(repository);
Stuart McCullochbb014372012-06-07 21:57:32 +000047 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);
Stuart McCulloch2286f232012-06-15 13:27:53 +000068 Parameters exports = project.parseHeader(manifest.getMainAttributes()
69 .getValue(Constants.EXPORT_PACKAGE));
Stuart McCullochbb014372012-06-07 21:57:32 +000070 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
Stuart McCulloch2286f232012-06-15 13:27:53 +000088 }
89 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +000090 main.close();
91 src.close();
92 }
93 }
94 return true;
95 }
96
97 private void split(Jar original, Jar main, Jar src) {
Stuart McCulloch2286f232012-06-15 13:27:53 +000098 for (Map.Entry<String,Resource> e : original.getResources().entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +000099 String path = e.getKey();
100 if (path.startsWith("OSGI-OPT/src/")) {
101 src.putResource(path.substring("OSGI-OPT/src/".length()), e.getValue());
102 } else {
103 main.putResource(path, e.getValue());
104 }
105 }
106 }
107
108 // gpg:sign-and-deploy-file \
109 // -Durl=http://oss.sonatype.org/service/local/staging/deploy/maven2
110 // \
111 // -DrepositoryId=sonatype-nexus-staging \
112 // -DupdateReleaseInfo=true \
113 // -DpomFile=pom.xml \
114 // -Dfile=/Ws/bnd/biz.aQute.bndlib/tmp/biz.aQute.bndlib.jar \
115 // -Dpassphrase=a1k3v3t5x3
116
Stuart McCulloch2286f232012-06-15 13:27:53 +0000117 private void maven_gpg_sign_and_deploy(Project b, File file, String classifier, File pomFile) throws Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +0000118 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 StringBuilder stdout = new StringBuilder();
132 StringBuilder stderr = new StringBuilder();
133
134 int result = command.execute(stdout, stderr);
135 if (result != 0) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000136 b.error("Maven deploy to %s failed to sign and transfer %s because %s", repository, file, "" + stdout
137 + stderr);
Stuart McCullochbb014372012-06-07 21:57:32 +0000138 }
139 }
140
Stuart McCullochd4826102012-06-26 16:34:24 +0000141 private void optional(Command command, @SuppressWarnings("unused") String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000142 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();
Stuart McCulloch2286f232012-06-15 13:27:53 +0000150
Stuart McCullochbb014372012-06-07 21:57:32 +0000151 command.add(b.getProperty("javadoc", "javadoc"));
152 command.add("-d");
153 command.add(tmp.getAbsolutePath());
154 command.add("-sourcepath");
Stuart McCulloch2286f232012-06-15 13:27:53 +0000155 command.add(Processor.join(b.getSourcePath(), File.pathSeparator));
Stuart McCullochbb014372012-06-07 21:57:32 +0000156
157 for (String packageName : exports) {
158 command.add(packageName);
159 }
160
161 StringBuilder out = new StringBuilder();
162 StringBuilder err = new StringBuilder();
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);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000180 }
181 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000182 out.close();
183 }
184 return f;
185 }
186
187}