| 44 | | public class KauriWebappMojo extends AbstractMojo { |
|---|
| 45 | | /** |
|---|
| 46 | | * Location of the kauri.xml. |
|---|
| 47 | | * |
|---|
| 48 | | * @parameter |
|---|
| 49 | | * @required |
|---|
| 50 | | */ |
|---|
| 51 | | protected String kauriConfigLocation; |
|---|
| 52 | | |
|---|
| 53 | | /** |
|---|
| 54 | | * Maven Project Builder component. |
|---|
| 55 | | * |
|---|
| 56 | | * @component |
|---|
| 57 | | */ |
|---|
| 58 | | protected MavenProjectBuilder projectBuilder; |
|---|
| 59 | | |
|---|
| 60 | | /** |
|---|
| 61 | | * Maven Artifact Factory component. |
|---|
| 62 | | * |
|---|
| 63 | | * @component |
|---|
| 64 | | */ |
|---|
| 65 | | protected ArtifactFactory artifactFactory; |
|---|
| 66 | | |
|---|
| 67 | | |
|---|
| 68 | | /** |
|---|
| 69 | | * Remote repositories used for the project. |
|---|
| 70 | | * |
|---|
| 71 | | * @parameter expression="${project.remoteArtifactRepositories}" |
|---|
| 72 | | * @required |
|---|
| 73 | | * @readonly |
|---|
| 74 | | */ |
|---|
| 75 | | protected List remoteRepositories; |
|---|
| 76 | | |
|---|
| 77 | | /** |
|---|
| 78 | | * Local Repository. |
|---|
| 79 | | * |
|---|
| 80 | | * @parameter expression="${localRepository}" |
|---|
| 81 | | * @required |
|---|
| 82 | | * @readonly |
|---|
| 83 | | */ |
|---|
| 84 | | protected ArtifactRepository localRepository; |
|---|
| 85 | | |
|---|
| 86 | | /** |
|---|
| 87 | | * Artifact Resolver component. |
|---|
| 88 | | * |
|---|
| 89 | | * @component |
|---|
| 90 | | */ |
|---|
| 91 | | protected ArtifactResolver resolver; |
|---|
| 92 | | |
|---|
| 93 | | /** |
|---|
| 94 | | * Artifact Resolver component. |
|---|
| 95 | | * |
|---|
| 96 | | * @component |
|---|
| 97 | | */ |
|---|
| 98 | | protected ArtifactMetadataSource metadataSource; |
|---|
| 99 | | |
|---|
| 100 | | /** |
|---|
| 101 | | * The Kauri version to employ. |
|---|
| 102 | | * |
|---|
| 103 | | * @parameter |
|---|
| 104 | | */ |
|---|
| 105 | | protected String kauriVersion; |
|---|
| | 25 | public class KauriWebappMojo extends AbstractPackageMojo { |
|---|
| 162 | | } |
|---|
| 163 | | |
|---|
| 164 | | private Set<Artifact> getModuleArtifactsFromKauriConfig() throws MojoExecutionException { |
|---|
| 165 | | Document kauriConfigDoc; |
|---|
| 166 | | try { |
|---|
| 167 | | File kauriConfigFile = new File(kauriConfigLocation); |
|---|
| 168 | | FileInputStream fis = null; |
|---|
| 169 | | try { |
|---|
| 170 | | fis = new FileInputStream(kauriConfigFile); |
|---|
| 171 | | kauriConfigDoc = parse(fis); |
|---|
| 172 | | } finally { |
|---|
| 173 | | if (fis != null) |
|---|
| 174 | | fis.close(); |
|---|
| 175 | | } |
|---|
| 176 | | } catch (Exception e) { |
|---|
| 177 | | throw new MojoExecutionException("Error reading kauri XML configuration from " + kauriConfigLocation, e); |
|---|
| 178 | | } |
|---|
| 179 | | |
|---|
| 180 | | return getArtifacts(kauriConfigDoc, "/runtime/modules/artifact", "Kauri configuration"); |
|---|
| 181 | | } |
|---|
| 182 | | |
|---|
| 183 | | |
|---|
| 184 | | private Document parse(InputStream is) throws ParserConfigurationException, IOException, SAXException { |
|---|
| 185 | | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|---|
| 186 | | dbf.setNamespaceAware(true); |
|---|
| 187 | | return dbf.newDocumentBuilder().parse(is); |
|---|
| 188 | | } |
|---|
| 189 | | |
|---|
| 190 | | private void determineKauriVersion() throws MojoExecutionException { |
|---|
| 191 | | String pomPropsPath = "META-INF/maven/org.kauriproject/kauri-webapp-plugin/pom.properties"; |
|---|
| 192 | | InputStream is = KauriWebappMojo.class.getClassLoader().getResourceAsStream(pomPropsPath); |
|---|
| 193 | | if (is == null) { |
|---|
| 194 | | throw new MojoExecutionException("Could not find the resource containing the Kauri version information at " + pomPropsPath); |
|---|
| 195 | | } |
|---|
| 196 | | |
|---|
| 197 | | Properties pomProps = new Properties(); |
|---|
| 198 | | try { |
|---|
| 199 | | pomProps.load(is); |
|---|
| 200 | | } catch (IOException e) { |
|---|
| 201 | | throw new MojoExecutionException("Error reading pom properties from " + pomPropsPath, e); |
|---|
| 202 | | } |
|---|
| 203 | | this.kauriVersion = pomProps.getProperty("version"); |
|---|
| 204 | | } |
|---|
| 205 | | |
|---|
| 206 | | private void createRepository(Set<Artifact> artifacts) throws MojoExecutionException { |
|---|
| 207 | | getLog().info("Creating the webapp-embedded artifact repository containing " + artifacts.size() + " artifacts."); |
|---|
| 208 | | for (Artifact artifact : artifacts) { |
|---|
| 209 | | String groupPath = artifact.getGroupId().replaceAll("\\.", Matcher.quoteReplacement("/")); |
|---|
| 210 | | File targetDir = new File(webappDirectory + "/WEB-INF/repository/" + groupPath + "/" |
|---|
| 211 | | + artifact.getArtifactId() + "/" + artifact.getVersion() + "/"); |
|---|
| 212 | | targetDir.mkdirs(); |
|---|
| 213 | | String fileName = artifact.getArtifactId() + "-" + artifact.getVersion(); |
|---|
| 214 | | if(artifact.hasClassifier()) { |
|---|
| 215 | | fileName += "-" + artifact.getClassifier(); |
|---|
| 216 | | } |
|---|
| 217 | | copyFile(artifact.getFile(), new File(targetDir, fileName + ".jar")); |
|---|
| 218 | | } |
|---|
| 219 | | } |
|---|
| 220 | | |
|---|
| 221 | | private void copyFile(File fromFile, File toFile) throws MojoExecutionException { |
|---|
| 222 | | try { |
|---|
| 223 | | FileInputStream srcFis = new FileInputStream(fromFile); |
|---|
| 224 | | FileOutputStream destFos = new FileOutputStream(toFile); |
|---|
| 225 | | FileChannel srcChannel = srcFis.getChannel(); |
|---|
| 226 | | FileChannel dstChannel = destFos.getChannel(); |
|---|
| 227 | | dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); |
|---|
| 228 | | srcChannel.close(); |
|---|
| 229 | | dstChannel.close(); |
|---|
| 230 | | srcFis.close(); |
|---|
| 231 | | destFos.close(); |
|---|
| 232 | | } catch (Throwable t) { |
|---|
| 233 | | throw new MojoExecutionException("Failed to copy file from " + fromFile + " to " + toFile); |
|---|
| 234 | | } |
|---|
| 235 | | } |
|---|
| 236 | | |
|---|
| 237 | | private Set<Artifact> getClassPathArtifacts(Artifact moduleArtifact) throws MojoExecutionException { |
|---|
| 238 | | String entryPath = "KAURI-INF/classloader.xml"; |
|---|
| 239 | | ZipFile zipFile = null; |
|---|
| 240 | | InputStream is = null; |
|---|
| 241 | | Document classLoaderDocument; |
|---|
| 242 | | try { |
|---|
| 243 | | zipFile = new ZipFile(moduleArtifact.getFile()); |
|---|
| 244 | | ZipEntry zipEntry = zipFile.getEntry(entryPath); |
|---|
| 245 | | if (zipEntry == null) { |
|---|
| 246 | | getLog().debug("No " + entryPath + " found in " + moduleArtifact); |
|---|
| 247 | | return Collections.emptySet(); |
|---|
| 248 | | } else { |
|---|
| 249 | | is = zipFile.getInputStream(zipEntry); |
|---|
| 250 | | classLoaderDocument = parse(is); |
|---|
| 251 | | } |
|---|
| 252 | | } catch (Exception e) { |
|---|
| 253 | | throw new MojoExecutionException("Error reading " + entryPath + " from " + moduleArtifact, e); |
|---|
| 254 | | } finally { |
|---|
| 255 | | if (is != null) |
|---|
| 256 | | try { is.close(); } catch (Exception e) { /* ignore */ } |
|---|
| 257 | | if (zipFile != null) |
|---|
| 258 | | try { zipFile.close(); } catch (Exception e) { /* ignore */ } |
|---|
| 259 | | } |
|---|
| 260 | | |
|---|
| 261 | | return getArtifacts(classLoaderDocument, "/classloader/classpath/artifact", "classloader.xml from module " + moduleArtifact); |
|---|
| 262 | | } |
|---|
| 263 | | |
|---|
| 264 | | private Set<Artifact> getArtifacts(Document configDoc, String artifactXPath, String sourceDescr) throws MojoExecutionException { |
|---|
| 265 | | Set<Artifact> artifacts = new HashSet<Artifact>(); |
|---|
| 266 | | NodeList nodeList; |
|---|
| 267 | | try { |
|---|
| 268 | | nodeList = (NodeList)xpathFactory.newXPath().evaluate(artifactXPath, configDoc, XPathConstants.NODESET); |
|---|
| 269 | | } catch (XPathExpressionException e) { |
|---|
| 270 | | throw new MojoExecutionException("Error resolving XPath expression " + artifactXPath + " on " + sourceDescr); |
|---|
| 271 | | } |
|---|
| 272 | | for (int i = 0; i < nodeList.getLength(); i++) { |
|---|
| 273 | | Element el = (Element)nodeList.item(i); |
|---|
| 274 | | String groupId = el.getAttribute("groupId"); |
|---|
| 275 | | String artifactId = el.getAttribute("artifactId"); |
|---|
| 276 | | String version = el.getAttribute("version"); |
|---|
| 277 | | String classifier = el.getAttribute("classifier"); |
|---|
| 278 | | if (version.equals("") && groupId.startsWith("org.kauriproject")) |
|---|
| 279 | | version = kauriVersion; |
|---|
| 280 | | if (classifier.equals("")) |
|---|
| 281 | | classifier = null; |
|---|
| 282 | | |
|---|
| 283 | | Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, "jar", classifier); |
|---|
| 284 | | if (!artifacts.contains(artifact)) { |
|---|
| 285 | | try { |
|---|
| 286 | | resolver.resolve(artifact, remoteRepositories, localRepository); |
|---|
| 287 | | } catch (Exception e) { |
|---|
| 288 | | throw new MojoExecutionException("Error resolving artifact listed in " + sourceDescr + ": " + artifact, e); |
|---|
| 289 | | } |
|---|
| 290 | | artifacts.add(artifact); |
|---|
| 291 | | } |
|---|
| 292 | | } |
|---|
| 293 | | |
|---|
| 294 | | return artifacts; |
|---|