blob: 592b3f989a2ec1cba38b6557df6e218b225ee948 [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.*;
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00008import aQute.bnd.header.*;
9import aQute.bnd.osgi.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000010import aQute.bnd.service.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000011import aQute.libg.command.*;
Stuart McCulloch81d48de2012-06-29 19:23:09 +000012import aQute.service.reporter.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000013
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 */
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +000043 public boolean deploy(Project project, String jarName, InputStream jarStream) 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
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +000050 project.progress("deploying %s to Maven repo: %s", jarName, repository);
Stuart McCullochbb014372012-06-07 21:57:32 +000051 File target = project.getTarget();
52 File tmp = Processor.getFile(target, repository);
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +000053 if (!tmp.exists() && !tmp.mkdirs()) {
54 throw new IOException("Could not create directory " + tmp);
55 }
Stuart McCullochbb014372012-06-07 21:57:32 +000056
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +000057 Jar original = new Jar(jarName, jarStream);
58 try {
59 Manifest manifest = original.getManifest();
60 if (manifest == null)
61 project.error("Jar has no manifest: %s", original);
62 else {
63 project.progress("Writing pom.xml");
64 PomResource pom = new PomResource(manifest);
65 pom.setProperties(maven);
66 File pomFile = write(tmp, pom, "pom.xml");
Stuart McCullochbb014372012-06-07 21:57:32 +000067
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +000068 Jar main = new Jar("main");
69 Jar src = new Jar("src");
70 try {
71 split(original, main, src);
72 Parameters exports = project.parseHeader(manifest.getMainAttributes().getValue(
73 Constants.EXPORT_PACKAGE));
74 File jdoc = new File(tmp, "jdoc");
75 jdoc.mkdirs();
76 project.progress("Generating Javadoc for: " + exports.keySet());
77 Jar javadoc = javadoc(jdoc, project, exports.keySet());
78 project.progress("Writing javadoc jar");
79 File javadocFile = write(tmp, new JarResource(javadoc), "javadoc.jar");
80 project.progress("Writing main file");
81 File mainFile = write(tmp, new JarResource(main), "main.jar");
82 project.progress("Writing sources file");
83 File srcFile = write(tmp, new JarResource(main), "src.jar");
Stuart McCullochbb014372012-06-07 21:57:32 +000084
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +000085 project.progress("Deploying main file");
86 maven_gpg_sign_and_deploy(project, mainFile, null, pomFile);
87 project.progress("Deploying main sources file");
88 maven_gpg_sign_and_deploy(project, srcFile, "sources", null);
89 project.progress("Deploying main javadoc file");
90 maven_gpg_sign_and_deploy(project, javadocFile, "javadoc", null);
Stuart McCullochbb014372012-06-07 21:57:32 +000091
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +000092 }
93 finally {
94 main.close();
95 src.close();
96 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000097 }
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +000098 }
99 finally {
100 original.close();
Stuart McCullochbb014372012-06-07 21:57:32 +0000101 }
102 return true;
103 }
104
105 private void split(Jar original, Jar main, Jar src) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000106 for (Map.Entry<String,Resource> e : original.getResources().entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000107 String path = e.getKey();
108 if (path.startsWith("OSGI-OPT/src/")) {
109 src.putResource(path.substring("OSGI-OPT/src/".length()), e.getValue());
110 } else {
111 main.putResource(path, e.getValue());
112 }
113 }
114 }
115
116 // gpg:sign-and-deploy-file \
117 // -Durl=http://oss.sonatype.org/service/local/staging/deploy/maven2
118 // \
119 // -DrepositoryId=sonatype-nexus-staging \
120 // -DupdateReleaseInfo=true \
121 // -DpomFile=pom.xml \
122 // -Dfile=/Ws/bnd/biz.aQute.bndlib/tmp/biz.aQute.bndlib.jar \
123 // -Dpassphrase=a1k3v3t5x3
124
Stuart McCulloch2286f232012-06-15 13:27:53 +0000125 private void maven_gpg_sign_and_deploy(Project b, File file, String classifier, File pomFile) throws Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +0000126 Command command = new Command();
127 command.setTrace();
128 command.add(b.getProperty("mvn", "mvn"));
129 command.add("gpg:sign-and-deploy-file", "-DreleaseInfo=true", "-DpomFile=pom.xml");
130 command.add("-Dfile=" + file.getAbsolutePath());
131 command.add("-DrepositoryId=" + repository);
132 command.add("-Durl=" + url);
133 optional(command, "passphrase", passphrase);
134 optional(command, "keyname", keyname);
135 optional(command, "homedir", homedir);
136 optional(command, "classifier", classifier);
137 optional(command, "pomFile", pomFile == null ? null : pomFile.getAbsolutePath());
138
139 StringBuilder stdout = new StringBuilder();
140 StringBuilder stderr = new StringBuilder();
141
142 int result = command.execute(stdout, stderr);
143 if (result != 0) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000144 b.error("Maven deploy to %s failed to sign and transfer %s because %s", repository, file, "" + stdout
145 + stderr);
Stuart McCullochbb014372012-06-07 21:57:32 +0000146 }
147 }
148
Stuart McCullochd4826102012-06-26 16:34:24 +0000149 private void optional(Command command, @SuppressWarnings("unused") String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000150 if (value == null)
151 return;
152
153 command.add("-D=" + value);
154 }
155
156 private Jar javadoc(File tmp, Project b, Set<String> exports) throws Exception {
157 Command command = new Command();
Stuart McCulloch2286f232012-06-15 13:27:53 +0000158
Stuart McCullochbb014372012-06-07 21:57:32 +0000159 command.add(b.getProperty("javadoc", "javadoc"));
160 command.add("-d");
161 command.add(tmp.getAbsolutePath());
162 command.add("-sourcepath");
Stuart McCulloch2286f232012-06-15 13:27:53 +0000163 command.add(Processor.join(b.getSourcePath(), File.pathSeparator));
Stuart McCullochbb014372012-06-07 21:57:32 +0000164
165 for (String packageName : exports) {
166 command.add(packageName);
167 }
168
169 StringBuilder out = new StringBuilder();
170 StringBuilder err = new StringBuilder();
171 Command c = new Command();
172 c.setTrace();
173 int result = c.execute(out, err);
174 if (result == 0) {
175 Jar jar = new Jar(tmp);
176 b.addClose(jar);
177 return jar;
178 }
179 b.error("Error during execution of javadoc command: %s / %s", out, err);
180 return null;
181 }
182
183 private File write(File base, Resource r, String fileName) throws Exception {
184 File f = Processor.getFile(base, fileName);
185 OutputStream out = new FileOutputStream(f);
186 try {
187 r.write(out);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000188 }
189 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000190 out.close();
191 }
192 return f;
193 }
194
195}