Search in sources :

Example 1 with SCRDescriptorGenerator

use of org.apache.felix.scrplugin.SCRDescriptorGenerator in project felix by apache.

the class SCRDescriptorBndPlugin method analyzeJar.

/**
 * Scan scr or ds annotation from the target jar.
 */
public boolean analyzeJar(Analyzer analyzer) throws Exception {
    this.log = new BndLog(reporter, analyzer, parseOption(properties, LOGTOFILE, false));
    try {
        init(analyzer);
        log.info("Analyzing " + analyzer.getBsn());
        final org.apache.felix.scrplugin.Project project = new org.apache.felix.scrplugin.Project();
        project.setClassLoader(new URLClassLoader(getClassPath(analyzer), this.getClass().getClassLoader()));
        project.setDependencies(getDependencies(analyzer));
        project.setSources(getClassFiles(analyzer));
        project.setClassesDirectory(destDir.getAbsolutePath());
        // create options
        final Options options = new Options();
        options.setOutputDirectory(destDir);
        options.setGenerateAccessors(generateAccessor);
        options.setStrictMode(strictMode);
        options.setProperties(new HashMap<String, String>());
        options.setSpecVersion(specVersion);
        final SCRDescriptorGenerator generator = new SCRDescriptorGenerator(log);
        // setup from plugin configuration
        generator.setOptions(options);
        generator.setProject(project);
        Result r = generator.execute();
        // Embed scr descriptors in target jar
        List<String> scrFiles = r.getScrFiles();
        if (scrFiles != null) {
            StringBuilder sb = new StringBuilder();
            for (String scrFile : scrFiles) {
                log.info("SCR descriptor result file: " + scrFile);
                sb.append(scrFile);
                sb.append(",");
                putResource(analyzer, scrFile);
            }
            sb.setLength(sb.length() - 1);
            addServiceComponentHeader(analyzer, sb.toString());
        }
        // Embed metatype descriptors in target jar
        List<String> metaTypeFiles = r.getMetatypeFiles();
        if (metaTypeFiles != null) {
            for (String metaTypeFile : metaTypeFiles) {
                log.info("Meta Type result file: " + metaTypeFile);
                putResource(analyzer, metaTypeFile);
            }
        }
    } catch (Throwable t) {
        log.error("Got unexpected exception while analyzing", t);
    } finally {
        log.close();
    }
    // do not reanalyze bundle classpath because our plugin has not changed it.
    return false;
}
Also used : Options(org.apache.felix.scrplugin.Options) SCRDescriptorGenerator(org.apache.felix.scrplugin.SCRDescriptorGenerator) Result(org.apache.felix.scrplugin.Result) URLClassLoader(java.net.URLClassLoader)

Example 2 with SCRDescriptorGenerator

use of org.apache.felix.scrplugin.SCRDescriptorGenerator in project felix by apache.

the class SCRDescriptorTask method execute.

@Override
public void execute() throws BuildException {
    // ensure we know the source
    if (getImplicitFileSet().getDir() == null) {
        throw new BuildException("srcdir attribute must be set!", getLocation());
    }
    // while debugging
    final org.apache.felix.scrplugin.Log scrLog = new AntLog(this);
    scrLog.debug("SCRDescriptorTask Configuration");
    scrLog.debug("  implicitFileset: " + getImplicitFileSet());
    scrLog.debug("  outputDirectory: " + destdir);
    scrLog.debug("  classpath: " + classpath);
    scrLog.debug("  generateAccessors: " + generateAccessors);
    scrLog.debug("  strictMode: " + strictMode);
    scrLog.debug("  specVersion: " + specVersion);
    try {
        final Path classPath = createClasspath();
        final org.apache.felix.scrplugin.Project project = new org.apache.felix.scrplugin.Project();
        project.setClassLoader(getClassLoader(this.getClass().getClassLoader()));
        project.setDependencies(getDependencies(classPath));
        project.setSources(getSourceFiles(getImplicitFileSet()));
        project.setClassesDirectory(destdir.getAbsolutePath());
        // create options
        final Options options = new Options();
        options.setOutputDirectory(destdir);
        options.setGenerateAccessors(generateAccessors);
        options.setStrictMode(strictMode);
        options.setProperties(new HashMap<String, String>());
        options.setSpecVersion(SpecVersion.fromName(specVersion));
        if (specVersion != null && options.getSpecVersion() == null) {
            throw new BuildException("Unknown spec version specified: " + specVersion);
        }
        final SCRDescriptorGenerator generator = new SCRDescriptorGenerator(scrLog);
        // setup from plugin configuration
        generator.setOptions(options);
        generator.setProject(project);
        generator.execute();
    } catch (final SCRDescriptorException sde) {
        if (sde.getSourceLocation() != null) {
            final Location loc = new Location(sde.getSourceLocation(), -1, 0);
            throw new BuildException(sde.getMessage(), sde.getCause(), loc);
        }
        throw new BuildException(sde.getMessage(), sde.getCause());
    } catch (SCRDescriptorFailureException sdfe) {
        throw new BuildException(sdfe.getMessage(), sdfe.getCause());
    }
}
Also used : Path(org.apache.tools.ant.types.Path) Options(org.apache.felix.scrplugin.Options) SCRDescriptorFailureException(org.apache.felix.scrplugin.SCRDescriptorFailureException) SCRDescriptorGenerator(org.apache.felix.scrplugin.SCRDescriptorGenerator) Project(org.apache.tools.ant.Project) BuildException(org.apache.tools.ant.BuildException) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException) Location(org.apache.tools.ant.Location)

Example 3 with SCRDescriptorGenerator

use of org.apache.felix.scrplugin.SCRDescriptorGenerator in project felix by apache.

the class SCRDescriptorMojo method execute.

@Override
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);
    }
}
Also used : Options(org.apache.felix.scrplugin.Options) SCRDescriptorFailureException(org.apache.felix.scrplugin.SCRDescriptorFailureException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SCRDescriptorGenerator(org.apache.felix.scrplugin.SCRDescriptorGenerator) Result(org.apache.felix.scrplugin.Result) Project(org.apache.felix.scrplugin.Project) MavenProject(org.apache.maven.project.MavenProject) URLClassLoader(java.net.URLClassLoader) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException)

Aggregations

Options (org.apache.felix.scrplugin.Options)3 SCRDescriptorGenerator (org.apache.felix.scrplugin.SCRDescriptorGenerator)3 URLClassLoader (java.net.URLClassLoader)2 Result (org.apache.felix.scrplugin.Result)2 SCRDescriptorException (org.apache.felix.scrplugin.SCRDescriptorException)2 SCRDescriptorFailureException (org.apache.felix.scrplugin.SCRDescriptorFailureException)2 Project (org.apache.felix.scrplugin.Project)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 MavenProject (org.apache.maven.project.MavenProject)1 BuildException (org.apache.tools.ant.BuildException)1 Location (org.apache.tools.ant.Location)1 Project (org.apache.tools.ant.Project)1 Path (org.apache.tools.ant.types.Path)1