Index: tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/CreateKarMojo.java =================================================================== --- tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/CreateKarMojo.java (revision 1333765) +++ tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/CreateKarMojo.java (working copy) @@ -22,6 +22,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.karaf.deployer.kar.KarArtifactInstaller; import org.apache.karaf.features.BundleInfo; @@ -40,6 +42,8 @@ import org.apache.maven.artifact.repository.metadata.SnapshotVersion; import org.apache.maven.artifact.repository.metadata.Versioning; import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.codehaus.plexus.archiver.jar.JarArchiver; @@ -47,7 +51,7 @@ /** * assembles a kar archive * - * @version $Revision: 1.1 $ + * @version $Revision$ * @goal features-create-kar * @phase package * @requiresDependencyResolution runtime @@ -104,7 +108,7 @@ * * @parameter default-value="${project.build.directory}/feature/feature.xml" */ - private File featuresFile; + private String featuresFile; /** @@ -115,14 +119,34 @@ private String repositoryPath = "repository/"; + private static final Pattern mvnPattern = Pattern.compile("mvn:([^/ ]+)/([^/ ]+)/([^/ ]*)(/([^/ ]+)(/([^/ ]+))?)?"); + // // Mojo // public void execute() throws MojoExecutionException, MojoFailureException { - List resources = readResources(); + File featuresFileResolved = resolveFile(featuresFile); + String groupId = project.getGroupId(); + String artifactId = project.getArtifactId(); + String version = project.getVersion(); + + if (isMavenUrl(featuresFile)) { + Artifact artifactTemp = resourceToArtifact(featuresFile, false); + + if (artifactTemp.getGroupId() != null) + groupId = artifactTemp.getGroupId(); + if (artifactTemp.getArtifactId() != null) + artifactId = artifactTemp.getArtifactId(); + if (artifactTemp.getVersion() != null) + version = artifactTemp.getVersion(); + } + + getLog().warn("FINAL FILE: " + featuresFileResolved); + + List resources = readResources(featuresFileResolved); // Build the archive - File archive = createArchive(resources); + File archive = createArchive(resources, featuresFileResolved, groupId, artifactId, version); // Attach the generated archive for install/deploy Artifact artifact = factory.createArtifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), null, "kar"); @@ -130,6 +154,35 @@ project.addAttachedArtifact(artifact); } + + private File resolveFile(String file) { + File fileResolved = null; + + if (isMavenUrl(file)) { + fileResolved = new File(fromMaven(file)); + + try { + Artifact artifactTemp = resourceToArtifact(file, false); + + if (!fileResolved.exists()) { + try { + resolver.resolve(artifactTemp, remoteRepos, localRepo); + fileResolved = artifactTemp.getFile(); + } catch (ArtifactResolutionException e) { + getLog().error("Artifact was not resolved", e); + } catch (ArtifactNotFoundException e) { + getLog().error("Artifact was not found", e); + } + } + } catch (MojoExecutionException e) { + getLog().error(e); + } + } else { + fileResolved = new File(file); + } + + return fileResolved; + } /** * Read bundles and configuration files in the features file. @@ -137,14 +190,14 @@ * @return * @throws MojoExecutionException */ - private List readResources() throws MojoExecutionException { + private List readResources(File featuresFile) throws MojoExecutionException { List resources = new ArrayList(); try { InputStream in = new FileInputStream(featuresFile); try { Features features = JaxbUtil.unmarshal(in, false); for (Feature feature : features.getFeature()) { - for (BundleInfo bundle : feature.getBundles()) { + for (BundleInfo bundle : feature.getBundles()) { if (!bundle.isDependency()) { resources.add(resourceToArtifact(bundle.getLocation(), false)); } @@ -153,6 +206,17 @@ resources.add(resourceToArtifact(configFile.getLocation(), false)); } } + + /* + if (addTransitiveFeatures) { + //remoteRepos.addAll(project.getRemoteArtifactRepositories()); + + for (String repository : features.getRepository()) { + resources.addAll(readResources(resolveFile(repository))); + } + } + */ + return resources; } finally { in.close(); @@ -169,7 +233,7 @@ * * @param bundles */ - private File createArchive(List bundles) throws MojoExecutionException { + private File createArchive(List bundles, File featuresFile, String groupId, String artifactId, String version) throws MojoExecutionException { ArtifactRepositoryLayout layout = new DefaultRepositoryLayout(); File archiveFile = getArchiveFile(outputDirectory, finalName, null); @@ -196,7 +260,7 @@ // archive.addManifestEntry(Constants.BUNDLE_SYMBOLICNAME, project.getArtifactId()); //include the feature.xml - Artifact featureArtifact = factory.createArtifactWithClassifier(project.getGroupId(), project.getArtifactId(), project.getVersion(), "xml", KarArtifactInstaller.FEATURE_CLASSIFIER); + Artifact featureArtifact = factory.createArtifactWithClassifier(groupId, artifactId, version, "xml", KarArtifactInstaller.FEATURE_CLASSIFIER); jarArchiver.addFile(featuresFile, repositoryPath + layout.pathOf(featureArtifact)); if (featureArtifact.isSnapshot()) { @@ -276,7 +340,58 @@ throw new MojoExecutionException("Failed to create archive", e); } } + + protected static boolean isMavenUrl(String name) { + Matcher m = mvnPattern.matcher(name); + return m.matches(); + } + + /** + * Returns a path for an artifact. + * Input: path (no ':') returns path + * Input: converts to default repo location path + * type and classifier are optional. + * + * + * @param name input artifact info + * @return path as supplied or a default maven repo path + */ + private static String fromMaven(String name) { + Matcher m = mvnPattern.matcher(name); + if (!m.matches()) { + return name; + } + StringBuilder b = new StringBuilder(); + b.append(m.group(1)); + for (int i = 0; i < b.length(); i++) { + if (b.charAt(i) == '.') { + b.setCharAt(i, '/'); + } + } + b.append("/");//groupId + String artifactId = m.group(2); + String version = m.group(3); + String extension = m.group(5); + String classifier = m.group(7); + b.append(artifactId).append("/");//artifactId + b.append(version).append("/");//version + b.append(artifactId).append("-").append(version); + if (present(classifier)) { + b.append("-").append(classifier); + } else { + if (present(extension)) { + b.append(".").append(extension); + } else { + b.append(".jar"); + } + } + return b.toString(); + } + private static boolean present(String part) { + return part != null && !part.isEmpty(); + } + protected static File getArchiveFile(final File basedir, final String finalName, String classifier) { if (classifier == null) { classifier = "";