You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/01/15 10:42:29 UTC
[camel] 01/02: CAMEL-13055: Polished and optimized the
camelpackage-maven-plugin
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 734548c40bc9f86cd9ca19feea3c84f2a75dd277
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 15 10:53:53 2019 +0100
CAMEL-13055: Polished and optimized the camelpackage-maven-plugin
---
.../maven/packaging/PackageComponentMojo.java | 16 +++++++++----
.../maven/packaging/PackageDataFormatMojo.java | 22 ++++++++++++-----
.../camel/maven/packaging/PackageHelper.java | 2 +-
.../camel/maven/packaging/PackageLanguageMojo.java | 20 +++++++++++-----
.../camel/maven/packaging/PackageLegalMojo.java | 9 +++----
.../camel/maven/packaging/PackageOtherMojo.java | 28 ++++++++++------------
.../maven/packaging/PrepareComponentMojo.java | 12 ++++++----
7 files changed, 68 insertions(+), 41 deletions(-)
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageComponentMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageComponentMojo.java
index 3e6a398..de29a45 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageComponentMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageComponentMojo.java
@@ -90,7 +90,7 @@ public class PackageComponentMojo extends AbstractMojo {
prepareComponent(getLog(), project, projectHelper, buildDir, componentOutDir, buildContext);
}
- public static void prepareComponent(Log log, MavenProject project, MavenProjectHelper projectHelper, File buildDir, File componentOutDir, BuildContext buildContext) throws MojoExecutionException {
+ public static int prepareComponent(Log log, MavenProject project, MavenProjectHelper projectHelper, File buildDir, File componentOutDir, BuildContext buildContext) throws MojoExecutionException {
File camelMetaDir = new File(componentOutDir, "META-INF/services/org/apache/camel/");
@@ -101,7 +101,7 @@ public class PackageComponentMojo extends AbstractMojo {
}
if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/component")) {
- return;
+ return 0;
}
StringBuilder buffer = new StringBuilder();
@@ -145,8 +145,10 @@ public class PackageComponentMojo extends AbstractMojo {
}
}
- // we need to enrich the component json files with data we know have from this plugin
- enrichComponentJsonFiles(log, project, buildDir, components);
+ if (count > 0) {
+ // we need to enrich the component json files with data we know have from this plugin
+ enrichComponentJsonFiles(log, project, buildDir, components);
+ }
if (count > 0) {
Properties properties = new Properties();
@@ -176,7 +178,7 @@ public class PackageComponentMojo extends AbstractMojo {
// are the content the same?
if (existing.equals(properties)) {
log.debug("No component changes detected");
- return;
+ return count;
}
} catch (IOException e) {
// ignore
@@ -190,12 +192,16 @@ public class PackageComponentMojo extends AbstractMojo {
log.info("Generated " + outFile + " containing " + count + " Camel " + (count > 1 ? "components: " : "component: ") + names);
+ buildContext.refresh(outFile);
+
} catch (IOException e) {
throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
}
} else {
log.debug("No META-INF/services/org/apache/camel/component directory found. Are you sure you have created a Camel component?");
}
+
+ return count;
}
private static void enrichComponentJsonFiles(Log log, MavenProject project, File buildDir, Map<String, String> components) throws MojoExecutionException {
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java
index d7f2361..afbd13b 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java
@@ -31,7 +31,6 @@ import java.util.Map;
import java.util.Properties;
import org.apache.maven.artifact.Artifact;
-import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -95,7 +94,7 @@ public class PackageDataFormatMojo extends AbstractMojo {
prepareDataFormat(getLog(), project, projectHelper, dataFormatOutDir, schemaOutDir, buildContext);
}
- public static void prepareDataFormat(Log log, MavenProject project, MavenProjectHelper projectHelper, File dataFormatOutDir,
+ public static int prepareDataFormat(Log log, MavenProject project, MavenProjectHelper projectHelper, File dataFormatOutDir,
File schemaOutDir, BuildContext buildContext) throws MojoExecutionException {
File camelMetaDir = new File(dataFormatOutDir, "META-INF/services/org/apache/camel/");
@@ -107,7 +106,7 @@ public class PackageDataFormatMojo extends AbstractMojo {
}
if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/dataformat")) {
- return;
+ return 0;
}
Map<String, String> javaTypes = new HashMap<>();
@@ -161,7 +160,9 @@ public class PackageDataFormatMojo extends AbstractMojo {
String json = loadText(is);
DataFormatModel dataFormatModel = extractDataFormatModel(project, json, modelName, name, javaType);
- log.debug("Model " + dataFormatModel);
+ if (log.isDebugEnabled()) {
+ log.debug("Model: " + dataFormatModel);
+ }
// build json schema for the data format
String properties = after(json, " \"properties\": {");
@@ -171,7 +172,9 @@ public class PackageDataFormatMojo extends AbstractMojo {
properties = prepareJsonProperties(name, properties);
String schema = createParameterJsonSchema(dataFormatModel, properties);
- log.debug("JSon schema\n" + schema);
+ if (log.isDebugEnabled()) {
+ log.debug("JSon schema:\n" + schema);
+ }
// write this to the directory
File dir = new File(schemaOutDir, schemaSubDirectory(dataFormatModel.getJavaType()));
@@ -181,6 +184,9 @@ public class PackageDataFormatMojo extends AbstractMojo {
OutputStream fos = buildContext.newFileOutputStream(out);
fos.write(schema.getBytes());
fos.close();
+
+ buildContext.refresh(out);
+
if (log.isDebugEnabled()) {
log.debug("Generated " + out + " containing JSon schema for " + name + " data format");
}
@@ -221,7 +227,7 @@ public class PackageDataFormatMojo extends AbstractMojo {
// are the content the same?
if (existing.equals(properties)) {
log.debug("No dataformat changes detected");
- return;
+ return count;
}
} catch (IOException e) {
// ignore
@@ -235,12 +241,16 @@ public class PackageDataFormatMojo extends AbstractMojo {
log.info("Generated " + outFile + " containing " + count + " Camel " + (count > 1 ? "dataformats: " : "dataformat: ") + names);
+ buildContext.refresh(outFile);
+
} catch (IOException e) {
throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
}
} else {
log.debug("No META-INF/services/org/apache/camel/dataformat directory found. Are you sure you have created a Camel data format?");
}
+
+ return count;
}
private static DataFormatModel extractDataFormatModel(MavenProject project, String json, String modelName, String name, String javaType) throws Exception {
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java
index 4bdc8f7..e342db6 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageHelper.java
@@ -48,7 +48,7 @@ public final class PackageHelper {
}
String path = file.getPath() + "/" + suffix;
if (log.isDebugEnabled()) {
- log.debug("checking if " + path + " (" + r.getDirectory() + "/" + suffix + ") has changed.");
+ log.debug("Checking if " + path + " (" + r.getDirectory() + "/" + suffix + ") has changed.");
}
if (buildContext.hasDelta(path)) {
log.debug("Indeed " + suffix + " has changed.");
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLanguageMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLanguageMojo.java
index 2e80c8c..4aca430 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLanguageMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLanguageMojo.java
@@ -31,7 +31,6 @@ import java.util.Map;
import java.util.Properties;
import org.apache.maven.artifact.Artifact;
-import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -95,7 +94,7 @@ public class PackageLanguageMojo extends AbstractMojo {
prepareLanguage(getLog(), project, projectHelper, languageOutDir, schemaOutDir, buildContext);
}
- public static void prepareLanguage(Log log, MavenProject project, MavenProjectHelper projectHelper, File languageOutDir,
+ public static int prepareLanguage(Log log, MavenProject project, MavenProjectHelper projectHelper, File languageOutDir,
File schemaOutDir, BuildContext buildContext) throws MojoExecutionException {
File camelMetaDir = new File(languageOutDir, "META-INF/services/org/apache/camel/");
@@ -107,7 +106,7 @@ public class PackageLanguageMojo extends AbstractMojo {
}
if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/language")) {
- return;
+ return 0;
}
Map<String, String> javaTypes = new HashMap<>();
@@ -201,12 +200,16 @@ public class PackageLanguageMojo extends AbstractMojo {
languageModel.setFirstVersion(row.get("firstVersion"));
}
}
- log.debug("Model " + languageModel);
+ if (log.isDebugEnabled()) {
+ log.debug("Model: " + languageModel);
+ }
// build json schema for the data format
String properties = after(json, " \"properties\": {");
String schema = createParameterJsonSchema(languageModel, properties);
- log.debug("JSon schema\n" + schema);
+ if (log.isDebugEnabled()) {
+ log.debug("JSon schema\n" + schema);
+ }
// write this to the directory
File dir = new File(schemaOutDir, schemaSubDirectory(languageModel.getJavaType()));
@@ -218,6 +221,7 @@ public class PackageLanguageMojo extends AbstractMojo {
fos.close();
buildContext.refresh(out);
+
if (log.isDebugEnabled()) {
log.debug("Generated " + out + " containing JSon schema for " + name + " language");
}
@@ -258,7 +262,7 @@ public class PackageLanguageMojo extends AbstractMojo {
// are the content the same?
if (existing.equals(properties)) {
log.debug("No language changes detected");
- return;
+ return count;
}
} catch (IOException e) {
// ignore
@@ -272,12 +276,16 @@ public class PackageLanguageMojo extends AbstractMojo {
log.info("Generated " + outFile + " containing " + count + " Camel " + (count > 1 ? "languages: " : "language: ") + names);
+ buildContext.refresh(outFile);
+
} catch (IOException e) {
throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
}
} else {
log.debug("No META-INF/services/org/apache/camel/language directory found. Are you sure you have created a Camel language?");
}
+
+ return count;
}
private static String readClassFromCamelResource(File file, StringBuilder buffer, BuildContext buildContext) throws MojoExecutionException {
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLegalMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLegalMojo.java
index 565cb70..917d180 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLegalMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageLegalMojo.java
@@ -42,7 +42,7 @@ import org.apache.maven.project.MavenProjectHelper;
import org.sonatype.plexus.build.incremental.BuildContext;
/**
- * Analyses the Camel plugins in a project and generates extra descriptor information for easier auto-discovery in Camel.
+ * Analyses the Camel plugins in a project and generates legal files.
*/
@Mojo(name = "generate-legal", threadSafe = true, defaultPhase = LifecyclePhase.PROCESS_CLASSES)
public class PackageLegalMojo extends AbstractMojo {
@@ -85,11 +85,12 @@ public class PackageLegalMojo extends AbstractMojo {
projectHelper.addResource(project, legalOutDir.getPath(), Collections.singletonList("**/*"), Collections.emptyList());
}
- public void processLegal(Path legalOutDir) {
+ public void processLegal(Path legalOutDir) throws MojoExecutionException {
// Only take care about camel legal stuff
if (!"org.apache.camel".equals(project.getGroupId())) {
return;
}
+
boolean hasLicense = project.getResources().stream()
.map(Resource::getDirectory)
.map(Paths::get)
@@ -100,7 +101,7 @@ public class PackageLegalMojo extends AbstractMojo {
String license = IOUtils.toString(isLicense, StandardCharsets.UTF_8);
updateResource(legalOutDir.resolve("META-INF").resolve("LICENSE.txt"), license);
} catch (IOException e) {
- throw new IOError(e);
+ throw new MojoExecutionException("Failed to write legal files. Reason: " + e, e);
}
}
boolean hasNotice = project.getResources().stream()
@@ -113,7 +114,7 @@ public class PackageLegalMojo extends AbstractMojo {
String notice = IOUtils.toString(isNotice, StandardCharsets.UTF_8);
updateResource(legalOutDir.resolve("META-INF").resolve("NOTICE.txt"), notice);
} catch (IOException e) {
- throw new IOError(e);
+ throw new MojoExecutionException("Failed to write legal files. Reason: " + e, e);
}
}
}
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageOtherMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageOtherMojo.java
index e4cdaa6..8b18fc1 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageOtherMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageOtherMojo.java
@@ -83,12 +83,6 @@ public class PackageOtherMojo extends AbstractMojo {
* @throws MojoFailureException something bad happened...
*/
public void execute() throws MojoExecutionException, MojoFailureException {
- prepareOthers(getLog(), project, projectHelper, otherOutDir, schemaOutDir, buildContext);
- }
-
- public static void prepareOthers(Log log, MavenProject project, MavenProjectHelper projectHelper, File otherOutDir,
- File schemaOutDir, BuildContext buildContext) throws MojoExecutionException {
-
File f = new File(project.getBasedir(), "target/classes");
File comp = new File(f, "META-INF/services/org/apache/camel/component");
if (comp.exists() && comp.isDirectory()) {
@@ -103,7 +97,11 @@ public class PackageOtherMojo extends AbstractMojo {
return;
}
- // okay none of those then this is a other kind of artifact
+ prepareOthers(getLog(), project, projectHelper, otherOutDir, schemaOutDir, buildContext);
+ }
+
+ public static void prepareOthers(Log log, MavenProject project, MavenProjectHelper projectHelper, File otherOutDir,
+ File schemaOutDir, BuildContext buildContext) throws MojoExecutionException {
// first we need to setup the output directory because the next check
// can stop the build before the end and eclipse always needs to know about that directory
@@ -111,12 +109,6 @@ public class PackageOtherMojo extends AbstractMojo {
projectHelper.addResource(project, otherOutDir.getPath(), Collections.singletonList("**/other.properties"), Collections.emptyList());
}
- if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/component")
- && !PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/dataformat")
- && !PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/language")) {
- return;
- }
-
String name = project.getArtifactId();
// strip leading camel-
if (name.startsWith("camel-")) {
@@ -144,7 +136,9 @@ public class PackageOtherMojo extends AbstractMojo {
}
otherModel.setTitle(title);
- log.debug("Model " + otherModel);
+ if (log.isDebugEnabled()) {
+ log.debug("Model: " + otherModel);
+ }
// write this to the directory
File dir = schemaOutDir;
@@ -158,7 +152,9 @@ public class PackageOtherMojo extends AbstractMojo {
buildContext.refresh(out);
- log.debug("Generated " + out + " containing JSon schema for " + name + " other");
+ if (log.isDebugEnabled()) {
+ log.debug("Generated " + out + " containing JSon schema for " + name + " other");
+ }
} catch (Exception e) {
throw new MojoExecutionException("Error loading other model. Reason: " + e, e);
}
@@ -206,6 +202,8 @@ public class PackageOtherMojo extends AbstractMojo {
log.info("Generated " + outFile);
+ buildContext.refresh(outFile);
+
} catch (IOException e) {
throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
}
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareComponentMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareComponentMojo.java
index e0642f3..98f55cd 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareComponentMojo.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareComponentMojo.java
@@ -115,10 +115,14 @@ public class PrepareComponentMojo extends AbstractMojo {
* @throws org.apache.maven.plugin.MojoFailureException something bad happened...
*/
public void execute() throws MojoExecutionException, MojoFailureException {
- prepareComponent(getLog(), project, projectHelper, buildDir, componentOutDir, buildContext);
- prepareDataFormat(getLog(), project, projectHelper, dataFormatOutDir, schemaOutDir, buildContext);
- prepareLanguage(getLog(), project, projectHelper, languageOutDir, schemaOutDir, buildContext);
- prepareOthers(getLog(), project, projectHelper, otherOutDir, schemaOutDir, buildContext);
+ int count = 0;
+ count += prepareComponent(getLog(), project, projectHelper, buildDir, componentOutDir, buildContext);
+ count += prepareDataFormat(getLog(), project, projectHelper, dataFormatOutDir, schemaOutDir, buildContext);
+ count += prepareLanguage(getLog(), project, projectHelper, languageOutDir, schemaOutDir, buildContext);
+ if (count == 0) {
+ // okay its not any of the above then its other
+ prepareOthers(getLog(), project, projectHelper, otherOutDir, schemaOutDir, buildContext);
+ }
}
}