Example usage for org.apache.maven.project MavenProject setContextValue

List of usage examples for org.apache.maven.project MavenProject setContextValue

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject setContextValue.

Prototype

public void setContextValue(String key, Object value) 

Source Link

Document

Sets the value of the context value of this project identified by the given key.

Usage

From source file:org.sourcepit.b2.internal.maven.ModelContextAdapterFactory.java

License:Apache License

public ModelContext adapt(MavenSession session, MavenProject project) {
    synchronized (project) {
        ModelContext context = (ModelContext) project.getContextValue(ModelContext.class.getName());
        if (context == null) {
            context = createModuleModelContext(session, project);
            project.setContextValue(ModelContext.class.getName(), context);
        }//from  w w  w .j a va 2 s  .  co  m
        return context;
    }
}

From source file:org.sourcepit.b2.maven.core.B2MavenBridge.java

License:Apache License

private void connect(MavenSession mavenSession, ResourceSet resourceSet) {
    final Map<File, Project> dirToProjectMap = new HashMap<File, Project>();
    for (MavenProject mavenProject : mavenSession.getProjects()) {
        if (isModuleProject(resourceSet, mavenProject)) {
            if (getContextValue(mavenProject, AbstractModule.class) != null) {
                throw new IllegalStateException("b2 maven bridge already connected");
            }/* w w w .  ja v a 2 s  . c o m*/

            final URI uri = B2MavenBridge.toArtifactURI(mavenProject, "module", null);
            final AbstractModule module = (AbstractModule) resourceSet.getResource(uri, true).getContents()
                    .get(0);

            mavenProject.setContextValue(AbstractModule.class.getName(), module);

            for (ProjectFacet<Project> projectFacet : module.getFacets(ProjectFacet.class)) {
                for (Project project : projectFacet.getProjects()) {
                    dirToProjectMap.put(project.getDirectory(), project);
                }
            }

            final File moduleDir = module.getDirectory();
            final File fileFlagsFile = new File(moduleDir, ".b2/moduleDirectory.properties");

            final ModuleDirectory moduleDirectory;
            if (fileFlagsFile.exists()) {
                moduleDirectory = ModuleDirectory.load(moduleDir, fileFlagsFile);
            } else {
                moduleDirectory = new ModuleDirectory(moduleDir, Collections.<File, Integer>emptyMap());
            }

            mavenProject.setContextValue(ModuleDirectory.class.getName(), moduleDirectory);
        }
    }

    for (MavenProject mavenProject : mavenSession.getProjects()) {
        final Project project = dirToProjectMap.get(mavenProject.getBasedir());
        if (project != null) {
            mavenProject.setContextValue(Project.class.getName(), project);
        }
    }
}

From source file:org.sourcepit.b2.maven.core.B2MavenBridge.java

License:Apache License

public void disconnect(MavenSession mavenSession) {
    for (MavenProject mavenProject : mavenSession.getProjects()) {
        mavenProject.setContextValue(AbstractModule.class.getName(), null);
        mavenProject.setContextValue(Project.class.getName(), null);
    }//from  www  .j  a v a 2  s . co  m
}

From source file:org.sourcepit.b2.release.B2ReleaseHelper.java

License:Apache License

public MavenProject adaptModuleProject(MavenProject reactorProject) {
    MavenProject moduleProject = (MavenProject) reactorProject.getContextValue("b2.moduleProject");
    if (moduleProject == null) {
        if (bridge.getModule(reactorProject) != null) {
            moduleProject = (MavenProject) reactorProject.clone();
            moduleProject.setFile(new File(reactorProject.getBasedir(), "module.xml"));
            reactorProject.setContextValue("b2.moduleProject", moduleProject);
        }/*from w w w  .  jav  a 2  s .c om*/
    }
    return moduleProject;
}

From source file:org.sourcepit.b2.release.B2ScmHelper.java

License:Apache License

private static Set<File> getCommitProposals(MavenProject mavenProject) {
    @SuppressWarnings("unchecked")
    Set<File> files = (Set<File>) mavenProject.getContextValue(CTX_KEY_COMMIT_PROPOSALS);
    if (files == null) {
        files = new LinkedHashSet<File>();
        mavenProject.setContextValue(CTX_KEY_COMMIT_PROPOSALS, files);
    }/* w  ww.j a  v  a2  s  .  co m*/
    return files;
}

From source file:org.sourcepit.maven.bootstrap.core.AbstractBootstrapper.java

License:Apache License

private List<?> discoverBootstrapParticipants(MavenSession bootSession, MavenProject bootProject,
        ClassRealm bootExtensionClassRealm) {
    final String key = BootstrapParticipant.class.getName() + "@" + bootExtensionClassRealm.getId();

    List<?> bootstrapParticipants = (List<?>) bootProject.getContextValue(key);
    if (bootstrapParticipants == null) {
        bootstrapParticipants = lookupBootstrapParticipants(bootSession, bootProject, bootExtensionClassRealm);
        bootProject.setContextValue(key, bootstrapParticipants);
    }//w w  w  .jav a 2 s .  c o  m
    return bootstrapParticipants;
}

From source file:org.sourcepit.osgifier.maven.GenerateManifestMojo.java

License:Apache License

@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException {
    final MavenProject project = buildContext.getSession().getCurrentProject();

    final ArtifactManifestBuilderRequest request = newManifestRequest(project);

    final ArtifactManifestBuilderResult result = manifestBuilder.buildManifest(request);
    project.setContextValue("osgifier.manifestBuilderResult", result);

    final BundleManifest manifest = result.getBundleManifest();
    write(bundleOutputDirectory, manifest, result.getBundleLocalization());

    final BundleManifest sourceManifest = result.getSourceBundleManifest();
    if (sourceManifest != null) {
        write(sourceBundleOutputDirectory, sourceManifest, result.getSourceBundleLocalization());
    }//from ww w.j a v  a2 s  .  c o  m

    if (pde) {
        // set as derived
        try {
            copyDirectory(bundleOutputDirectory, project.getBasedir());
        } catch (IOException e) {
            throw pipe(e);
        }
    }
}

From source file:org.sourcepit.osgifier.maven.OsgifyArtifactsMojo.java

License:Apache License

@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException {
    final PropertiesSource options = MojoUtils
            .getOptions(buildContext.getSession().getCurrentProject().getProperties(), this.options);
    final Date startTime = buildContext.getSession().getStartTime();

    final OsgifierContext osgifyContext = modelBuilder.build(new DefaultOsgifierContextInflatorFilter(),
            options, artifacts, startTime);

    final Collection<BundleCandidate> bundles = new ArrayList<BundleCandidate>();
    for (BundleCandidate bundle : osgifyContext.getBundles()) {
        if (!bundle.isNativeBundle() && bundle.getTargetBundle() == null) {
            bundles.add(bundle);/* w  w w  . j a  va2  s .c  o m*/
        }
    }

    final DependencyModel dependencyModel = osgifyContext.getExtension(DependencyModel.class);

    final Map<BundleCandidate, MavenProject> mavenProjectMapping = new HashMap<BundleCandidate, MavenProject>(
            bundles.size());

    for (BundleCandidate bundle : bundles) {
        final MavenArtifact artifact = bundle.getExtension(MavenArtifact.class);

        try {
            ProjectBuildingResult result = buildMavenProject(artifact);
            mavenProjectMapping.put(bundle, result.getProject());
        } catch (ProjectBuildingException e) {
            getLog().warn(String.format(
                    "Failed to resolve original Maven model for artifact %s. Some information may note be available in genrated pom.",
                    artifact), e);
        }

        adoptMavenCoordinates(dependencyModel, bundle);
        final BundleCandidate sourceBundle = bundle.getSourceBundle();
        if (sourceBundle != null) {
            adoptMavenCoordinates(dependencyModel, sourceBundle);
        }
    }

    for (BundleCandidate bundle : bundles) {
        injectManifest(dependencyModel, bundle);
        final BundleCandidate sourceBundle = bundle.getSourceBundle();
        if (sourceBundle != null) {
            injectManifest(dependencyModel, sourceBundle);
        }
    }

    ModelUtils.writeModel(getOsgifierContextFile(), osgifyContext);

    final Collection<Artifact> artifacts = new ArrayList<Artifact>();
    for (BundleCandidate bundle : bundles) {
        final MavenArtifact bundleArtifact = bundle.getExtension(MavenArtifact.class);
        final Artifact artifact = artifactFactory.createArtifact(bundleArtifact.getArtifactKey())
                .setFile(bundle.getLocation());
        artifacts.add(artifact);

        final BundleCandidate sourceBundle = bundle.getSourceBundle();
        if (sourceBundle != null) {
            final Artifact sourceArtifact = artifactFactory.createArtifact(artifact, "sources", "jar")
                    .setFile(sourceBundle.getLocation());
            artifacts.add(sourceArtifact);
        }

        final Model model = buildPom(bundle, dependencyModel);

        final MavenProject mavenProject = mavenProjectMapping.get(bundle);
        if (mavenProject != null) {
            final Model oriModel = mavenProject.getModel();

            model.setCiManagement(oriModel.getCiManagement());
            model.setContributors(oriModel.getContributors());
            model.setDescription(oriModel.getDescription());
            model.setDevelopers(oriModel.getDevelopers());
            model.setInceptionYear(oriModel.getInceptionYear());
            model.setLicenses(oriModel.getLicenses());
            model.setMailingLists(oriModel.getMailingLists());
            model.setName(oriModel.getName());
            model.setOrganization(oriModel.getOrganization());
            model.setScm(oriModel.getScm());
            model.setUrl(oriModel.getUrl());
        }

        final File pomFile = new File(workDir, getBundleId(bundle) + ".xml");
        try {
            new DefaultModelWriter().write(pomFile, null, model);
        } catch (IOException e) {
            throw pipe(e);
        }

        final Artifact pomArtifact = artifactFactory.createArtifact(artifact, null, "pom").setFile(pomFile);
        artifacts.add(pomArtifact);
    }

    MavenProject project = buildContext.getSession().getCurrentProject();
    project.setContextValue("osgified-artifacts", artifacts);

    printSummary(artifacts);
}

From source file:org.sourcepit.tpmp.resolver.tycho.TychoSessionTargetPlatformResolver.java

License:Apache License

private MavenProject setupAggregatedProject(MavenSession session,
        TargetPlatformConfiguration aggregatedConfiguration) {
    PropertiesMap mvnProperties = new LinkedPropertiesMap();
    mvnProperties.load(getClass().getClassLoader(), "META-INF/tpmp/maven.properties");

    String groupId = mvnProperties.get("groupId");
    String artifactId = mvnProperties.get("artifactId");
    String version = mvnProperties.get("version");

    final String tpmpKey = Plugin.constructKey(groupId, artifactId);

    MavenProject origin = session.getCurrentProject();

    Model model = origin.getModel().clone();
    Build build = model.getBuild();//from   ww w  .  j a va  2 s.co  m
    if (build.getPluginsAsMap().get(tpmpKey) == null) {
        Plugin tpmp = new Plugin();
        tpmp.setGroupId(groupId);
        tpmp.setArtifactId(artifactId);
        tpmp.setVersion(version);

        build.getPlugins().add(tpmp);
        build.flushPluginMap();
    }

    MavenProject fake = new MavenProject(model);
    fake.setClassRealm(origin.getClassRealm());
    fake.setFile(origin.getFile());

    final Map<String, ArtifactRepository> artifactRepositories = new LinkedHashMap<String, ArtifactRepository>();
    final Map<String, ArtifactRepository> pluginRepositories = new LinkedHashMap<String, ArtifactRepository>();
    for (MavenProject project : session.getProjects()) {
        for (ArtifactRepository repository : project.getRemoteArtifactRepositories()) {
            if (!artifactRepositories.containsKey(repository.getId())) {
                artifactRepositories.put(repository.getId(), repository);
            }
        }
        for (ArtifactRepository repository : project.getPluginArtifactRepositories()) {
            if (!pluginRepositories.containsKey(repository.getId())) {
                pluginRepositories.put(repository.getId(), repository);
            }
        }
    }

    fake.setRemoteArtifactRepositories(new ArrayList<ArtifactRepository>(artifactRepositories.values()));
    fake.setPluginArtifactRepositories(new ArrayList<ArtifactRepository>(pluginRepositories.values()));
    fake.setManagedVersionMap(origin.getManagedVersionMap());

    if (getTychoProject(fake) == null) {
        fake.setPackaging("eclipse-repository");
    }

    fake.getBuildPlugins();

    AbstractTychoProject tychoProject = (AbstractTychoProject) getTychoProject(fake);
    tychoProject.setupProject(session, fake);

    Properties properties = new Properties();
    properties.putAll(fake.getProperties());
    properties.putAll(session.getSystemProperties()); // session wins
    properties.putAll(session.getUserProperties());
    fake.setContextValue(TychoConstants.CTX_MERGED_PROPERTIES, properties);

    fake.setContextValue(TychoConstants.CTX_TARGET_PLATFORM_CONFIGURATION, aggregatedConfiguration);

    ExecutionEnvironmentConfiguration eeConfiguration = new ExecutionEnvironmentConfigurationImpl(logger,
            aggregatedConfiguration.isResolveWithEEConstraints());
    tychoProject.readExecutionEnvironmentConfiguration(fake, eeConfiguration);
    fake.setContextValue(TychoConstants.CTX_EXECUTION_ENVIRONMENT_CONFIGURATION, eeConfiguration);

    final DependencyMetadata dm = new DependencyMetadata();
    for (ReactorProject reactorProject : DefaultReactorProject.adapt(session)) {
        mergeMetadata(dm, reactorProject, true);
        mergeMetadata(dm, reactorProject, false);
    }

    int i = 0;
    for (Object object : dm.getMetadata(true)) {
        InstallableUnitDAO dao = new TychoSourceIUResolver.InstallableUnitDAO(
                object.getClass().getClassLoader());
        dao.setProperty(object, RepositoryLayoutHelper.PROP_CLASSIFIER, "fake_" + i);
        i++;
    }

    for (Object object : dm.getMetadata(false)) {
        InstallableUnitDAO dao = new TychoSourceIUResolver.InstallableUnitDAO(
                object.getClass().getClassLoader());
        dao.setProperty(object, RepositoryLayoutHelper.PROP_CLASSIFIER, "fake_" + i);
        i++;
    }

    Map<String, DependencyMetadata> metadata = new LinkedHashMap<String, DependencyMetadata>();
    metadata.put(null, dm);

    fake.setContextValue("tpmp.aggregatedMetadata", metadata);

    return fake;
}