org.apache.felix.scrplugin.mojo.SCRDescriptorMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-scr-plugin Show documentation
Show all versions of maven-scr-plugin Show documentation
Maven plugin for generating OSGi service descriptors based on annotations.
/*
* 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.felix.scrplugin.mojo;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scrplugin.Options;
import org.apache.felix.scrplugin.Project;
import org.apache.felix.scrplugin.Result;
import org.apache.felix.scrplugin.SCRDescriptorException;
import org.apache.felix.scrplugin.SCRDescriptorFailureException;
import org.apache.felix.scrplugin.SCRDescriptorGenerator;
import org.apache.felix.scrplugin.Source;
import org.apache.felix.scrplugin.SpecVersion;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.model.Plugin;
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.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.sonatype.plexus.build.incremental.BuildContext;
/**
* The SCRDescriptorMojo
generates a service descriptor file based
* on annotations found in the sources.
*
* @goal scr
* @phase process-classes
* @threadSafe
* @description Build Service Descriptors from Java Source
* @requiresDependencyResolution compile
*/
public class SCRDescriptorMojo extends AbstractMojo {
/**
* The groupID of the SCR Annotation library
*
* @see #SCR_ANN_MIN_VERSION
* @see #checkAnnotationArtifact(Artifact)
*/
private static final String SCR_ANN_GROUPID = "org.apache.felix";
/**
* The artifactID of the SCR Annotation library.
*
* @see #SCR_ANN_MIN_VERSION
* @see #checkAnnotationArtifact(Artifact)
*/
private static final String SCR_ANN_ARTIFACTID = "org.apache.felix.scr.annotations";
private static final String BUNDLE_PLUGIN_GROUP_ID = "org.apache.felix";
private static final String BUNDLE_PLUGIN_ARTIFACT_ID = "maven-bundle-plugin";
private static final String BUNDLE_PLUGIN_INSTRUCTIONS = "instructions";
private static final String BUNDLE_PLUGIN_EXTENSION = "BNDExtension-";
private static final String OSGI_CFG_RESOURCES = "Include-Resource";
private static final String OSGI_CFG_COMPONENTS = "Service-Component";
/**
* The minimum SCR Annotation library version supported by this plugin. See
* FELIX-2680 for full details.
*
* @see #checkAnnotationArtifact(Artifact)
*/
private static final ArtifactVersion SCR_ANN_MIN_VERSION = new DefaultArtifactVersion(
"1.9.0");
/**
* @parameter expression="${project.build.outputDirectory}"
* @required
* @readonly
*/
private File outputDirectory;
/**
* The Maven project.
*
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* This flag controls the generation of the bind/unbind methods.
*
* @parameter default-value="true"
*/
private boolean generateAccessors;
/**
* In strict mode the plugin even fails on warnings.
*
* @parameter default-value="false"
*/
private boolean strictMode;
/**
* The comma separated list of tokens to include when processing sources.
*
* @parameter alias="includes"
*/
private String sourceIncludes;
/**
* The comma separated list of tokens to exclude when processing sources.
*
* @parameter alias="excludes"
*/
private String sourceExcludes;
/**
* Predefined properties.
*
* @parameter
*/
private Map properties = new LinkedHashMap();
/**
* The version of the DS spec this plugin generates a descriptor for. By
* default the version is detected by the used tags.
*
* @parameter
*/
private String specVersion;
/**
* Project types which this plugin supports.
*
* @parameter
*/
private List supportedProjectTypes = Arrays.asList( new String[]
{ "jar", "bundle" } );
/**
* By default the plugin scans the java source tree, if this is set to true,
* the generated classes directory is scanned instead.
*
* @parameter default-value="false"
*/
private boolean scanClasses;
/**
* Skip volatile check for fields.
*
* @parameter default-value="false"
*/
private boolean skipVolatileCheck;
/**
* @component
*/
private BuildContext buildContext;
public void execute() throws MojoExecutionException, MojoFailureException {
final String projectType = project.getArtifact().getType();
// ignore unsupported project types, useful when bundleplugin is configured in parent pom
if ( !supportedProjectTypes.contains( projectType ) ) {
getLog().debug(
"Ignoring project type " + projectType + " - supportedProjectTypes = " + supportedProjectTypes );
return;
}
// create the log for the generator
final org.apache.felix.scrplugin.Log scrLog = new MavenLog(getLog(), buildContext);
// create project
final MavenProjectScanner scanner = new MavenProjectScanner(
this.buildContext,
this.project, this.sourceIncludes, this.sourceExcludes, this.scanClasses, scrLog);
final Project project = new Project();
// create the class loader
project.setClassLoader(new URLClassLoader(getClassPath(), this
.getClass().getClassLoader()));
project.setDependencies(scanner.getDependencies());
project.setSources(scanner.getSources());
project.setClassesDirectory(this.project.getBuild().getOutputDirectory());
// create options
final Options options = new Options();
options.setOutputDirectory(outputDirectory);
options.setGenerateAccessors(generateAccessors);
options.setStrictMode(strictMode);
options.setProperties(properties);
options.setSpecVersion(SpecVersion.fromName(specVersion));
options.setIncremental(this.buildContext.isIncremental());
options.setSkipVolatileCheck(this.skipVolatileCheck);
if ( specVersion != null && options.getSpecVersion() == null ) {
throw new MojoExecutionException("Unknown spec version specified: " + specVersion);
}
try {
final SCRDescriptorGenerator generator = new SCRDescriptorGenerator(
scrLog);
// setup from plugin configuration
generator.setOptions(options);
generator.setProject(project);
this.removePossiblyStaleFiles(scanner.getSources(), options);
final Result result = generator.execute();
this.setServiceComponentHeader(options);
if ( !this.updateProjectResources() ) {
this.setIncludeResourceHeader(options);
}
this.cleanUpDeletedSources(scanner.getDeletedSources(), options);
this.refreshMessages(result.getProcessedSourceFiles());
this.updateBuildContext(result);
} catch (final SCRDescriptorException sde) {
throw new MojoExecutionException(sde.getSourceLocation() + " : " + sde.getMessage(), sde);
} catch (final SCRDescriptorFailureException sdfe) {
throw (MojoFailureException) new MojoFailureException(
sdfe.getMessage()).initCause(sdfe);
}
}
/**
* Remove existing files for the sources which have recently changed
*
* This method ensures that files which were generated in a previous run are not
* leftover if the source file has changed by:
*
* - No longer having a @Componentannotation
* - No longer having the metatype property set to true
*
*
*
* @param sources the changed source files
*/
private void removePossiblyStaleFiles(final Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy