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

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

Introduction

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

Prototype

public File getFile() 

Source Link

Usage

From source file:org.sonar.batch.scan.maven.MavenProjectConverter.java

License:Open Source License

public static ProjectDefinition convert(List<MavenProject> poms, MavenProject root) {
    // projects by canonical path to pom.xml
    Map<String, MavenProject> paths = Maps.newHashMap();
    Map<MavenProject, ProjectDefinition> defs = Maps.newHashMap();

    try {//from  w ww.  j av a2 s.co  m
        for (MavenProject pom : poms) {
            paths.put(pom.getFile().getCanonicalPath(), pom);
            defs.put(pom, convert(pom));
        }

        for (Map.Entry<String, MavenProject> entry : paths.entrySet()) {
            MavenProject pom = entry.getValue();
            for (Object m : pom.getModules()) {
                String moduleId = (String) m;
                File modulePath = new File(pom.getBasedir(), moduleId);
                MavenProject module = findMavenProject(modulePath, paths);

                ProjectDefinition parentProject = defs.get(pom);
                if (parentProject == null) {
                    throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
                }
                ProjectDefinition subProject = defs.get(module);
                if (subProject == null) {
                    throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
                }
                parentProject.addSubProject(subProject);
            }
        }
    } catch (IOException e) {
        throw new SonarException(e);
    }

    ProjectDefinition rootProject = defs.get(root);
    if (rootProject == null) {
        throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
    }
    return rootProject;
}

From source file:org.sonar.maven3.Maven3PluginExecutor.java

License:Open Source License

@Override
public void concreteExecute(MavenProject pom, String goal) {
    MavenSession projectSession = mavenSession.clone();
    projectSession.setCurrentProject(pom);
    projectSession.setProjects(Arrays.asList(pom));
    projectSession.getRequest().setRecursive(false);
    projectSession.getRequest().setPom(pom.getFile());
    projectSession.getRequest().setGoals(Arrays.asList(goal));
    projectSession.getRequest().setInteractiveMode(false);
    lifecycleExecutor.execute(projectSession);
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private static MavenProject findMavenProject(final File modulePath, Collection<MavenProject> modules)
        throws IOException {

    File canonical = modulePath.getCanonicalFile();
    for (MavenProject module : modules) {
        if (module.getBasedir().getCanonicalFile().equals(canonical)
                || module.getFile().getCanonicalFile().equals(canonical)) {
            return module;
        }/*from  w w  w  . ja va2 s  .  c o  m*/
    }
    return null;
}

From source file:org.sonarsource.scanner.maven.bootstrap.MavenProjectConverter.java

License:Open Source License

private List<File> mainSources(MavenProject pom) throws MojoExecutionException {
    Set<String> sources = new LinkedHashSet<>();
    if (MAVEN_PACKAGING_WAR.equals(pom.getModel().getPackaging())) {
        sources.add(MavenUtils.getPluginSetting(pom, MavenUtils.GROUP_ID_APACHE_MAVEN,
                ARTIFACTID_MAVEN_WAR_PLUGIN, "warSourceDirectory", "src/main/webapp"));
    }// w  ww  .  j  ava2  s  . co m

    sources.add(pom.getFile().getPath());
    sources.addAll(pom.getCompileSourceRoots());

    return sourcePaths(pom, ScanProperties.PROJECT_SOURCE_DIRS, sources);
}

From source file:org.sonatype.m2e.webby.internal.build.WebbyBuildParticipant.java

License:Open Source License

@Override
public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception {
    SubMonitor pm = SubMonitor.convert(monitor, "Creating WAR project output...", 100);
    try {//from  w  w  w . j  ava2 s.co m
        boolean incremental = kind != IncrementalProjectBuilder.FULL_BUILD;

        IMavenProjectFacade mvnFacade = getMavenProjectFacade();

        MavenProject mvnProject = mvnFacade.getMavenProject(pm.newChild(15));

        getBuildContext().removeMessages(mvnProject.getFile());

        MavenSession mvnSession = getSession();

        WarConfiguration warConfig = new WarConfigurationExtractor().getConfiguration(mvnFacade, mvnProject,
                mvnSession, pm.newChild(15));

        Map<String, Artifact> overlayArtifacts = WarUtils.getOverlayArtifacts(mvnProject);

        File warDir = new File(warConfig.getWarDirectory());
        IFolder warFolder = getFolder(warDir.getAbsolutePath());

        String[] files = warDir.list();
        if (files == null || files.length <= 0) {
            incremental = false;
        }

        IResourceDelta theDelta = incremental ? getDelta(mvnFacade.getProject()) : null;

        File warConfigFile = new File(warConfig.getWorkDirectory(), "config.ser");
        if (incremental && (theDelta == null
                || theDelta.findMember(mvnFacade.getPom().getProjectRelativePath()) != null)) {
            Object previousWarConfiguration = null;
            if (warConfigFile.isFile()) {
                try {
                    previousWarConfiguration = WarConfiguration.load(warConfigFile);
                } catch (IOException e) {
                    WebbyPlugin.log(e, IStatus.WARNING);
                }
            }
            if (!warConfig.equals(previousWarConfiguration)) {
                incremental = false;
                theDelta = null;
                if (warFolder != null) {
                    warFolder.delete(true, null);
                }
            }
        }
        if (!incremental || !warConfigFile.exists()) {
            try {
                warConfig.save(warConfigFile);
            } catch (IOException e) {
                WebbyPlugin.log(e, IStatus.WARNING);
            }
        }

        File resourceRegistryFile = new File(warConfig.getWorkDirectory(), "resources.ser");
        ResourceRegistry resourceRegistry = null;
        if (incremental) {
            Object obj = mvnFacade.getSessionProperty(PROP_WAR_RESOURCES);
            if (obj instanceof Reference) {
                obj = ((Reference<?>) obj).get();
            }
            if (obj instanceof ResourceRegistry) {
                resourceRegistry = (ResourceRegistry) obj;
            } else {
                if (resourceRegistryFile.isFile()) {
                    try {
                        resourceRegistry = ResourceRegistry.load(resourceRegistryFile);
                        mvnFacade.setSessionProperty(PROP_WAR_RESOURCES,
                                new SoftReference<Object>(resourceRegistry));
                    } catch (IOException e) {
                        WebbyPlugin.log(e, IStatus.WARNING);
                    }
                }
            }
        }
        if (resourceRegistry == null) {
            incremental = false;
            theDelta = null;
            resourceRegistry = new ResourceRegistry();
            mvnFacade.setSessionProperty(PROP_WAR_RESOURCES, new SoftReference<Object>(resourceRegistry));
        }

        Map<IProject, IResourceDelta> resDeltas = new IdentityHashMap<IProject, IResourceDelta>();
        List<ResourceContributor> resourceContributors = new ArrayList<ResourceContributor>();
        int overlayOrdinal = 0;
        for (OverlayConfiguration overlayConfig : warConfig.getOverlays()) {
            if (overlayConfig.isSkip()) {
                continue;
            }
            overlayOrdinal++;
            ResourceContributor resourceContributor;
            if (overlayConfig.isMain()) {
                resourceContributor = new MainResourceContributor(overlayOrdinal, mvnFacade, warConfig,
                        theDelta);
            } else {
                Artifact overlayArtifact = overlayArtifacts.get(overlayConfig.getArtifactKey());
                if (overlayArtifact == null) {
                    addConfigurationError(mvnProject,
                            "The overlay " + overlayConfig.getId() + " refers to a non-existing artifact");
                    continue;
                }
                IMavenProjectFacade overlayFacade = MavenUtils.getFacade(overlayArtifact.getGroupId(),
                        overlayArtifact.getArtifactId(), overlayArtifact.getBaseVersion());
                if (overlayFacade == null) {
                    if (overlayArtifact.getFile() == null) {
                        // unresolved, this should already have been reported by m2e core
                        continue;
                    }
                    if (incremental) {
                        continue;
                    }
                    resourceContributor = new ArtifactResourceContributor(overlayOrdinal,
                            overlayArtifact.getFile(), overlayConfig);
                } else {
                    IProject overlayProject = overlayFacade.getProject();
                    IResourceDelta resDelta;
                    if (resDeltas.containsKey(overlayProject)) {
                        resDelta = resDeltas.get(overlayProject);
                    } else {
                        resDelta = incremental ? getDelta(overlayProject) : null;
                        resDeltas.put(overlayProject, resDelta);
                    }
                    resourceContributor = new ProjectResourceContributor(overlayOrdinal, overlayFacade,
                            overlayConfig, resDelta);
                }
            }
            resourceContributors.add(resourceContributor);
        }

        FilteringHandler filteringHandler = new FilteringHandler(warConfig, mvnProject, mvnSession);
        WarAssembler warAssembler = new WarAssembler(warDir, filteringHandler, resourceRegistry);

        SubMonitor spm = SubMonitor.convert(pm.newChild(65), resourceContributors.size());
        for (ResourceContributor resourceContributor : resourceContributors) {
            if (pm.isCanceled()) {
                throw new OperationCanceledException();
            }
            resourceContributor.contribute(warAssembler, spm.newChild(1));
        }

        try {
            resourceRegistry.save(resourceRegistryFile);
        } catch (IOException e) {
            WebbyPlugin.log(e, IStatus.WARNING);
        }

        if (warFolder != null) {
            warFolder.refreshLocal(IResource.DEPTH_INFINITE, pm.newChild(5));
        }

        return new HashSet<IProject>(resDeltas.keySet());
    } finally {
        if (monitor != null) {
            monitor.done();
        }
    }
}

From source file:org.sonatype.m2e.webby.internal.build.WebbyBuildParticipant.java

License:Open Source License

private void addConfigurationError(MavenProject mvnProject, String msg) {
    File pomFile = mvnProject.getFile();

    int line = 0;
    int column = 0;
    InputLocation location = new WarConfigurationExtractor().getConfigurationLocation(mvnProject);
    if (location != null && location.getSource() != null) {
        String modelId = mvnProject.getGroupId() + ":" + mvnProject.getArtifactId() + ':'
                + mvnProject.getVersion();
        if (location.getSource().getModelId().equals(modelId)) {
            line = location.getLineNumber();
            column = location.getColumnNumber();
        }/*  w  w  w  .ja  va 2  s.com*/
    }

    BuildContext buildContext = getBuildContext();
    buildContext.addMessage(pomFile, line, column, msg, BuildContext.SEVERITY_ERROR, null);
}

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

License:Apache License

public void performCheckins(ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
        List<MavenProject> mavenModuleProjects, String message)
        throws ReleaseScmRepositoryException, ReleaseExecutionException, ReleaseScmCommandException {
    ScmRepository repository;//  ww  w  .j a v a2 s. c  o m
    ScmProvider provider;
    try {
        repository = scmRepositoryConfigurator.getConfiguredRepository(releaseDescriptor,
                releaseEnvironment.getSettings());

        repository.getProviderRepository().setPushChanges(releaseDescriptor.isPushChanges());

        provider = scmRepositoryConfigurator.getRepositoryProvider(repository);
    } catch (ScmRepositoryException e) {
        throw new ReleaseScmRepositoryException(e.getMessage(), e.getValidationMessages());
    } catch (NoSuchScmProviderException e) {
        throw new ReleaseExecutionException("Unable to configure SCM repository: " + e.getMessage(), e);
    }

    if (releaseDescriptor.isCommitByProject()) {
        for (MavenProject project : mavenModuleProjects) {
            List<File> pomFiles = collectFiles(releaseDescriptor, project);
            ScmFileSet fileSet = new ScmFileSet(project.getFile().getParentFile(), pomFiles);

            checkin(provider, repository, fileSet, releaseDescriptor, message);
        }
    } else {
        List<File> pomFiles = collectFiles(releaseDescriptor, mavenModuleProjects);
        ScmFileSet fileSet = new ScmFileSet(new File(releaseDescriptor.getWorkingDirectory()), pomFiles);

        checkin(provider, repository, fileSet, releaseDescriptor, message);
    }
}

From source file:org.sourcepit.b2.release.phase.RewritePomsForBranchPhase.java

License:Apache License

private void markModuleXmlsForCommit(List<MavenProject> mavenModuleProjects) {
    for (MavenProject mavenProject : mavenModuleProjects) {
        scmHelper.markForCommit(mavenProject, mavenProject.getFile());
    }/*from w  w  w  .  j  a v  a  2  s .c  om*/
}

From source file:org.sourcepit.common.maven.core.MavenProjectUtils.java

License:Apache License

public static org.sourcepit.common.maven.model.MavenProject toMavenProject(
        @NotNull org.apache.maven.project.MavenProject mavenProject) {
    final org.sourcepit.common.maven.model.MavenProject mProject = MavenModelFactory.eINSTANCE
            .createMavenProject();/*from   w ww  .  ja  va2 s .com*/
    mProject.setGroupId(mavenProject.getGroupId());
    mProject.setArtifactId(mavenProject.getArtifactId());
    mProject.setVersion(mavenProject.getVersion());
    if (mavenProject.getPackaging() != null
            && !ObjectUtils.equals(mProject.getPackaging(), mavenProject.getPackaging())) {
        mProject.setPackaging(mavenProject.getPackaging());
    }
    mProject.setPomFile(mavenProject.getFile());
    mProject.setOutputDirectory(MavenProjectUtils.getOutputDir(mavenProject));
    mProject.setTestOutputDirectory(MavenProjectUtils.getTestOutputDir(mavenProject));
    return mProject;
}

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

License:Apache License

private Map<String, MavenProject> getProjectMap(List<MavenProject> projects)
        throws org.apache.maven.DuplicateProjectException {
    Map<String, MavenProject> index = new LinkedHashMap<String, MavenProject>();
    Map<String, List<File>> collisions = new LinkedHashMap<String, List<File>>();

    for (MavenProject project : projects) {
        String projectId = ArtifactUtils.key(project.getGroupId(), project.getArtifactId(),
                project.getVersion());/*from  w w w . j  a v  a 2s . com*/

        MavenProject collision = index.get(projectId);

        if (collision == null) {
            index.put(projectId, project);
        } else {
            List<File> pomFiles = collisions.get(projectId);

            if (pomFiles == null) {
                pomFiles = new ArrayList<File>(Arrays.asList(collision.getFile(), project.getFile()));
                collisions.put(projectId, pomFiles);
            } else {
                pomFiles.add(project.getFile());
            }
        }
    }

    if (!collisions.isEmpty()) {
        throw new org.apache.maven.DuplicateProjectException("Two or more projects in the reactor"
                + " have the same identifier, please make sure that <groupId>:<artifactId>:<version>"
                + " is unique for each project: " + collisions, collisions);
    }

    return index;
}