org.apache.camel.maven.packaging.PackageDataFormatMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-package-maven-plugin Show documentation
Show all versions of camel-package-maven-plugin Show documentation
Maven plugin to help package Camel components and plugins
/**
* 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.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
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;
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.PackageHelper.after;
import static org.apache.camel.maven.packaging.PackageHelper.loadText;
import static org.apache.camel.maven.packaging.PackageHelper.parseAsMap;
/**
* Analyses the Camel plugins in a project and generates extra descriptor information for easier auto-discovery in Camel.
*
* @goal generate-dataformats-list
*/
public class PackageDataFormatMojo extends AbstractMojo {
/**
* The maven project.
*
* @parameter property="project"
* @required
* @readonly
*/
protected MavenProject project;
/**
* The output directory for generated dataformats file
*
* @parameter default-value="${project.build.directory}/generated/camel/dataformats"
*/
protected File dataFormatOutDir;
/**
* The output directory for generated dataformats file
*
* @parameter default-value="${project.build.directory}/classes"
*/
protected File schemaOutDir;
/**
* 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 {
prepareDataFormat(getLog(), project, projectHelper, dataFormatOutDir, schemaOutDir);
}
public static void prepareDataFormat(Log log, MavenProject project, MavenProjectHelper projectHelper, File dataFormatOutDir, File schemaOutDir) throws MojoExecutionException {
File camelMetaDir = new File(dataFormatOutDir, "META-INF/services/org/apache/camel/");
Map javaTypes = new HashMap();
StringBuilder buffer = new StringBuilder();
int count = 0;
for (Resource r : project.getBuild().getResources()) {
File f = new File(r.getDirectory());
if (!f.exists()) {
f = new File(project.getBasedir(), r.getDirectory());
}
f = new File(f, "META-INF/services/org/apache/camel/dataformat");
if (f.exists() && f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (File file : files) {
// skip directories as there may be a sub .resolver directory
if (file.isDirectory()) {
continue;
}
String name = file.getName();
if (name.charAt(0) != '.') {
count++;
if (buffer.length() > 0) {
buffer.append(" ");
}
buffer.append(name);
}
// find out the javaType for each data format
try {
String text = loadText(new FileInputStream(file));
Map map = parseAsMap(text);
String javaType = map.get("class");
if (javaType != null) {
javaTypes.put(name, javaType);
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to read file " + file + ". Reason: " + e, e);
}
}
}
}
}
// find camel-core and grab the data format model from there, and enrich this model with information from this artifact
// and create json schema model file for this data format
try {
if (count > 0) {
Artifact camelCore = findCamelCoreArtifact(project);
if (camelCore != null) {
File core = camelCore.getFile();
if (core != null) {
URL url = new URL("file", null, core.getAbsolutePath());
URLClassLoader loader = new URLClassLoader(new URL[]{url});
for (Map.Entry entry : javaTypes.entrySet()) {
String name = entry.getKey();
String javaType = entry.getValue();
String modelName = asModelName(name);
InputStream is = loader.getResourceAsStream("org/apache/camel/model/dataformat/" + modelName + ".json");
if (is == null) {
// use file input stream if we build camel-core itself, and thus do not have a JAR which can be loaded by URLClassLoader
is = new FileInputStream(new File(core, "org/apache/camel/model/dataformat/" + modelName + ".json"));
}
String json = loadText(is);
if (json != null) {
DataFormatModel dataFormatModel = new DataFormatModel();
dataFormatModel.setName(name);
dataFormatModel.setTitle("");
dataFormatModel.setModelName(modelName);
dataFormatModel.setLabel("");
dataFormatModel.setDescription(project.getDescription());
dataFormatModel.setJavaType(javaType);
dataFormatModel.setGroupId(project.getGroupId());
dataFormatModel.setArtifactId(project.getArtifactId());
dataFormatModel.setVersion(project.getVersion());
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy