Skip to content

Commit

Permalink
CAMEL-13663: camel-main-maven-plugin to generte spring-boot tooling m…
Browse files Browse the repository at this point in the history
…etadata to fool Java editors to have code completions for Camel Main application.properties files.
  • Loading branch information
davsclaus committed Jun 19, 2019
1 parent 2f4df3c commit 43ffa83
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import org.apache.camel.util.IOHelper;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.exec.AbstractExecMojo;

/**
* Base class for maven goals.
*/
public abstract class AbstractMainMojo extends AbstractExecMojo {

/**
* Whether to log the classpath when starting
*/
@Parameter(property = "camel.logClasspath", defaultValue = "false")
protected boolean logClasspath;

/**
* Whether to allow downloading Camel catalog version from the internet. This is needed if the project
* uses a different Camel version than this plugin is using by default.
*/
@Parameter(property = "camel.downloadVersion", defaultValue = "true")
protected boolean downloadVersion;

protected transient ClassLoader classLoader;

@Component
private RepositorySystem repositorySystem;

@Component
private ArtifactResolver artifactResolver;

@Parameter(property = "localRepository")
private ArtifactRepository localRepository;

@Parameter(property = "project.remoteArtifactRepositories")
private List remoteRepositories;

protected static String findCamelVersion(MavenProject project) {
Dependency candidate = null;

List list = project.getDependencies();
for (Object obj : list) {
Dependency dep = (Dependency) obj;
if ("org.apache.camel".equals(dep.getGroupId())) {
if ("camel-core".equals(dep.getArtifactId())) {
// favor camel-core
candidate = dep;
break;
} else {
candidate = dep;
}
}
}
if (candidate != null) {
return candidate.getVersion();
}

return null;
}

protected Set<String> resolveCamelComponentsFromClasspath() throws MojoFailureException {
Set<String> components = new TreeSet<>();
try {
classLoader = getClassLoader();
Enumeration<URL> en = classLoader.getResources("META-INF/services/org/apache/camel/component.properties");
while (en.hasMoreElements()) {
URL url = en.nextElement();
InputStream is = (InputStream) url.getContent();
Properties prop = new Properties();
prop.load(is);
String comps = prop.getProperty("components");
if (comps != null) {
String[] parts = comps.split("\\s+");
components.addAll(Arrays.asList(parts));
}
IOHelper.close(is);
}
} catch (Throwable e) {
throw new MojoFailureException("Error during discovering Camel components from classpath due " + e.getMessage(), e);
}

return components;
}

/**
* Set up a classloader for scanning
*/
private ClassLoader getClassLoader() throws MalformedURLException, MojoExecutionException {
Set<URL> classpathURLs = new LinkedHashSet<>();

// add project classpath
URL mainClasses = new File(project.getBuild().getOutputDirectory()).toURI().toURL();
classpathURLs.add(mainClasses);

// add maven dependencies
Set<Artifact> deps = project.getArtifacts();
deps.addAll(getAllNonTestScopedDependencies());
for (Artifact dep : deps) {
File file = dep.getFile();
if (file != null) {
classpathURLs.add(file.toURI().toURL());
}
}

if (logClasspath) {
getLog().info("Classpath:");
for (URL url : classpathURLs) {
getLog().info(" " + url.getFile());
}
}
return new URLClassLoader(classpathURLs.toArray(new URL[classpathURLs.size()]));
}

private Collection<Artifact> getAllNonTestScopedDependencies() throws MojoExecutionException {
List<Artifact> answer = new ArrayList<>();

for (Artifact artifact : getAllDependencies()) {

// do not add test artifacts
if (!artifact.getScope().equals(Artifact.SCOPE_TEST)) {

if ("google-collections".equals(artifact.getArtifactId())) {
// skip this as we conflict with guava
continue;
}

if (!artifact.isResolved()) {
ArtifactResolutionRequest req = new ArtifactResolutionRequest();
req.setArtifact(artifact);
req.setResolveTransitively(true);
req.setLocalRepository(localRepository);
req.setRemoteRepositories(remoteRepositories);
artifactResolver.resolve(req);
}

answer.add(artifact);
}
}
return answer;
}

// generic method to retrieve all the transitive dependencies
private Collection<Artifact> getAllDependencies() {
List<Artifact> artifacts = new ArrayList<>();

for (Iterator<?> dependencies = project.getDependencies().iterator(); dependencies.hasNext(); ) {
Dependency dependency = (Dependency) dependencies.next();
Artifact art = repositorySystem.createDependencyArtifact(dependency);
artifacts.add(art);
}

return artifacts;
}
}

0 comments on commit 43ffa83

Please sign in to comment.