Repository: camel Updated Branches: refs/heads/master a4ea61e1e -> db04b50fd
CAMEL-8365: Generate archetype-catalog for Camel archetypes Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2c6bb87f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2c6bb87f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2c6bb87f Branch: refs/heads/master Commit: 2c6bb87f83dd7932e07c12930904e94174826148 Parents: a4ea61e Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Feb 18 19:39:27 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Feb 18 19:39:27 2015 +0100 ---------------------------------------------------------------------- tooling/archetypes/camel-archetype-scr/pom.xml | 103 ++++----- tooling/archetypes/pom.xml | 28 ++- .../packaging/PackageArchetypeCatalogMojo.java | 228 +++++++++++++++++++ .../camel/maven/packaging/StringHelper.java | 43 ++++ 4 files changed, 348 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2c6bb87f/tooling/archetypes/camel-archetype-scr/pom.xml ---------------------------------------------------------------------- diff --git a/tooling/archetypes/camel-archetype-scr/pom.xml b/tooling/archetypes/camel-archetype-scr/pom.xml index cba3734..6c4f25d 100644 --- a/tooling/archetypes/camel-archetype-scr/pom.xml +++ b/tooling/archetypes/camel-archetype-scr/pom.xml @@ -17,62 +17,63 @@ limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <parent> - <artifactId>archetypes</artifactId> - <groupId>org.apache.camel</groupId> - <version>2.15-SNAPSHOT</version> - </parent> + <parent> + <artifactId>archetypes</artifactId> + <groupId>org.apache.camel</groupId> + <version>2.15-SNAPSHOT</version> + </parent> - <groupId>org.apache.camel.archetypes</groupId> - <artifactId>camel-archetype-scr</artifactId> - <name>Camel :: Archetypes :: OSGi Service Component Runtime bundle</name> - <description>Creates a new Camel SCR bundle project for Karaf</description> - <packaging>maven-archetype</packaging> + <groupId>org.apache.camel.archetypes</groupId> + <artifactId>camel-archetype-scr</artifactId> + <name>Camel :: Archetypes :: OSGi Service Component Runtime bundle</name> + <description>Creates a new Camel SCR bundle project for Karaf</description> + <packaging>maven-archetype</packaging> - <build> - <extensions> - <extension> - <groupId>org.apache.maven.archetype</groupId> - <artifactId>archetype-packaging</artifactId> - <version>2.2</version> - </extension> - </extensions> + <build> + <extensions> + <extension> + <groupId>org.apache.maven.archetype</groupId> + <artifactId>archetype-packaging</artifactId> + <version>2.2</version> + </extension> + </extensions> - <resources> - <resource> - <directory>src/main/resources</directory> - <filtering>false</filtering> - </resource> - <resource> - <directory>src/main/resources-filtered</directory> - <filtering>true</filtering> - <includes> - <include>**/archetype-metadata.xml</include> - </includes> - </resource> - </resources> + <resources> + <resource> + <directory>src/main/resources</directory> + <filtering>false</filtering> + </resource> + <resource> + <directory>src/main/resources-filtered</directory> + <filtering>true</filtering> + <includes> + <include>**/archetype-metadata.xml</include> + </includes> + </resource> + </resources> - <testResources> - <testResource> - <directory>src/test/resources</directory> - <filtering>true</filtering> - </testResource> - </testResources> + <testResources> + <testResource> + <directory>src/test/resources</directory> + <filtering>true</filtering> + </testResource> + </testResources> - <pluginManagement> - <plugins> - <plugin> - <artifactId>maven-archetype-plugin</artifactId> - <version>2.2</version> - <configuration> - <skip>${maven.test.skip.exec}</skip> - </configuration> - </plugin> - </plugins> - </pluginManagement> - </build> + <pluginManagement> + <plugins> + <plugin> + <artifactId>maven-archetype-plugin</artifactId> + <version>2.2</version> + <configuration> + <skip>${maven.test.skip.exec}</skip> + </configuration> + </plugin> + </plugins> + </pluginManagement> + </build> </project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/2c6bb87f/tooling/archetypes/pom.xml ---------------------------------------------------------------------- diff --git a/tooling/archetypes/pom.xml b/tooling/archetypes/pom.xml index 8fe7c77..eb62fe4 100644 --- a/tooling/archetypes/pom.xml +++ b/tooling/archetypes/pom.xml @@ -15,7 +15,8 @@ See the License for the specific language governing permissions and limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> @@ -48,7 +49,27 @@ <module>camel-archetype-web</module> <module>camel-archetype-webconsole</module> </modules> + <build> + <plugins> + + <!-- generate archetype catalog --> + <plugin> + <groupId>org.apache.camel</groupId> + <artifactId>camel-package-maven-plugin</artifactId> + <version>${project.version}</version> + <executions> + <execution> + <goals> + <goal>generate-archetype-catalog</goal> + </goals> + <phase>process-resources</phase> + </execution> + </executions> + </plugin> + + </plugins> + <pluginManagement> <plugins> <plugin> @@ -56,10 +77,10 @@ <artifactId>maven-eclipse-plugin</artifactId> <configuration> <buildcommands> - <java.lang.String>com.atlassw.tools.eclipse.checkstyle.CheckstyleBuilder</java.lang.String> + <java.lang.String>com.atlassw.tools.eclipse.checkstyle.CheckstyleBuilder</java.lang.String> </buildcommands> <projectnatures> - <nature>com.atlassw.tools.eclipse.checkstyle.CheckstyleNature</nature> + <nature>com.atlassw.tools.eclipse.checkstyle.CheckstyleNature</nature> </projectnatures> <additionalConfig> <file> @@ -72,4 +93,5 @@ </plugins> </pluginManagement> </build> + </project> http://git-wip-us.apache.org/repos/asf/camel/blob/2c6bb87f/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageArchetypeCatalogMojo.java ---------------------------------------------------------------------- diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageArchetypeCatalogMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageArchetypeCatalogMojo.java new file mode 100644 index 0000000..16faa55 --- /dev/null +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageArchetypeCatalogMojo.java @@ -0,0 +1,228 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.maven.packaging; + +import java.io.File; +import java.io.FileFilter; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectHelper; + +import static org.apache.camel.maven.packaging.StringHelper.between; + +/** + * Creates the Maven catalog for the Camel archetypes + * + * @goal generate-archetype-catalog + * @execute phase="process-resources" + */ +public class PackageArchetypeCatalogMojo extends AbstractMojo { + + /** + * The maven project. + * + * @parameter property="project" + * @required + * @readonly + */ + protected MavenProject project; + + /** + * The output directory for generated components file + * + * @parameter default-value="${project.build.directory}/classes/" + */ + protected File outDir; + + /** + * Maven ProjectHelper. + * + * @component + * @readonly + */ + private MavenProjectHelper projectHelper; + + /** + * Execute goal. + * + * @throws org.apache.maven.plugin.MojoExecutionException execution of the main class or one of the + * threads it generated failed. + * @throws org.apache.maven.plugin.MojoFailureException something bad happened... + */ + public void execute() throws MojoExecutionException, MojoFailureException { + try { + generateArchetypeCatalog(getLog(), project, projectHelper, outDir); + } catch (IOException e) { + throw new MojoFailureException("Error generating archetype catalog due " + e.getMessage(), e); + } + } + + public static void generateArchetypeCatalog(Log log, MavenProject project, MavenProjectHelper projectHelper, File outDir) throws MojoExecutionException, IOException { + + // find all archetypes + File[] dirs = new File(".").listFiles(new FileFilter() { + @Override + public boolean accept(File pathname) { + // skip web console as its deprecated + if ("camel-archetype-webconsole".equals(pathname.getName())) { + return false; + } + return pathname.getName().startsWith("camel-archetype") && pathname.isDirectory(); + } + }); + + + List<ArchetypeModel> models = new ArrayList<ArchetypeModel>(); + + for (File dir : dirs) { + File pom = new File(dir, "pom.xml"); + if (!pom.exists() && !pom.isFile()) { + continue; + } + + boolean parent = false; + ArchetypeModel model = new ArchetypeModel(); + + // just use a simple line by line text parser (no need for DOM) just to grab 4 lines of data + for (Object o : FileUtils.readLines(pom)) { + + String line = o.toString(); + + // we only want to read version from parent + if (line.contains("<parent>")) { + parent = true; + continue; + } + if (line.contains("</parent>")) { + parent = false; + continue; + } + if (parent) { + // grab version from parent + String version = between(line, "<version>", "</version>"); + if (version != null) { + model.setVersion(version); + } + continue; + } + + String groupId = between(line, "<groupId>", "</groupId>"); + String artifactId = between(line, "<artifactId>", "</artifactId>"); + String description = between(line, "<description>", "</description>"); + + if (groupId != null && model.getGroupId() == null) { + model.setGroupId(groupId); + } + if (artifactId != null && model.getArtifactId() == null) { + model.setArtifactId(artifactId); + } + if (description != null && model.getDescription() == null) { + model.setDescription(description); + } + } + + if (model.getGroupId() != null && model.getArtifactId() != null && model.getVersion() != null) { + models.add(model); + } + } + + log.info("Found " + models.size() + " archetypes"); + + if (!models.isEmpty()) { + + // make sure there is a dir + outDir.mkdirs(); + + File out = new File(outDir, "archetype-catalog.xml"); + FileOutputStream fos = new FileOutputStream(out, false); + + // write top + String top = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<archetype-catalog>\n <archetypes>"; + fos.write(top.getBytes()); + + // write each archetype + for (ArchetypeModel model : models) { + fos.write("\n <archetype>".getBytes()); + fos.write(("\n <groupId>" + model.getGroupId() + "</groupId>").getBytes()); + fos.write(("\n <artifactId>" + model.getArtifactId() + "</artifactId>").getBytes()); + fos.write(("\n <version>" + model.getVersion() + "</version>").getBytes()); + if (model.getDescription() != null) { + fos.write(("\n <description>" + model.getDescription() + "</description>").getBytes()); + } + fos.write("\n </archetype>".getBytes()); + } + + // write bottom + String bottom = "\n </archetypes>\n</archetype-catalog>\n"; + fos.write(bottom.getBytes()); + + fos.close(); + + log.info("Saved archetype catalog to file " + out); + } + } + + private static class ArchetypeModel { + + private String groupId; + private String artifactId; + private String version; + private String description; + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getArtifactId() { + return artifactId; + } + + public void setArtifactId(String artifactId) { + this.artifactId = artifactId; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/2c6bb87f/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/StringHelper.java ---------------------------------------------------------------------- diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/StringHelper.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/StringHelper.java new file mode 100644 index 0000000..9af9ec9 --- /dev/null +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/StringHelper.java @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.maven.packaging; + +public class StringHelper { + + public static String after(String text, String after) { + if (!text.contains(after)) { + return null; + } + return text.substring(text.indexOf(after) + after.length()); + } + + public static String before(String text, String before) { + if (!text.contains(before)) { + return null; + } + return text.substring(0, text.indexOf(before)); + } + + public static String between(String text, String after, String before) { + text = after(text, after); + if (text == null) { + return null; + } + return before(text, before); + } + +}