List of usage examples for org.apache.maven.project MavenProject getBuild
public Build getBuild()
From source file:org.jboss.tools.arquillian.ui.internal.dialogs.ArquillianResourcesSelectionDialog.java
License:Open Source License
private void getResources() { allResources = new ArrayList<IPath>(); if (javaProject != null && javaProject.isOpen()) { IPath testSourcePath = null;//w w w . ja v a2 s.com try { IProject project = javaProject.getProject(); if (project.hasNature(IMavenConstants.NATURE_ID)) { IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project, new NullProgressMonitor()); MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor()); Build build = mavenProject.getBuild(); String testSourceDirectory = build.getTestSourceDirectory(); testSourcePath = Path.fromOSString(testSourceDirectory); IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation(); testSourcePath = testSourcePath.makeRelativeTo(workspacePath).makeAbsolute(); } IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); for (IClasspathEntry entry : rawClasspath) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry); if (roots == null) { continue; } for (IPackageFragmentRoot root : roots) { IPath path = root.getPath(); String projectLocation = project.getLocation().toOSString(); IPath projectPath = Path.fromOSString(projectLocation); IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation(); projectPath = projectPath.makeRelativeTo(workspacePath).makeAbsolute(); projectPath = projectPath.removeLastSegments(1); path = projectPath.append(path); if (path != null && path.equals(testSourcePath)) { continue; } Object[] resources = root.getNonJavaResources(); for (Object resource : resources) { addResource(allResources, resource, root.getPath()); } } } } } catch (Exception e1) { ArquillianUIActivator.log(e1); } } }
From source file:org.jboss.tools.arquillian.ui.internal.dialogs.ArquillianTypesSelectionDialog.java
License:Open Source License
private void getClasses() { allTypes = new ArrayList<IType>(); if (javaProject != null && javaProject.isOpen()) { IPath testSourcePath = null;/*from w w w .j a v a2 s.c o m*/ try { IProject project = javaProject.getProject(); if (project.hasNature(IMavenConstants.NATURE_ID)) { IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project, new NullProgressMonitor()); MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor()); Build build = mavenProject.getBuild(); String testSourceDirectory = build.getTestSourceDirectory(); testSourcePath = Path.fromOSString(testSourceDirectory); IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation(); testSourcePath = testSourcePath.makeRelativeTo(workspacePath).makeAbsolute(); } IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); for (IClasspathEntry entry : rawClasspath) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry); if (roots == null) { continue; } for (IPackageFragmentRoot root : roots) { IPath path = root.getPath(); String projectLocation = project.getLocation().toOSString(); IPath projectPath = Path.fromOSString(projectLocation); IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation(); projectPath = projectPath.makeRelativeTo(workspacePath).makeAbsolute(); projectPath = projectPath.removeLastSegments(1); path = projectPath.append(path); if (path != null && path.equals(testSourcePath)) { continue; } IJavaElement[] children = root.getChildren(); for (IJavaElement child : children) { if (child instanceof IPackageFragment) { IPackageFragment packageFragment = (IPackageFragment) child; IJavaElement[] elements = packageFragment.getChildren(); for (IJavaElement element : elements) { if (element instanceof ICompilationUnit) { ICompilationUnit cu = (ICompilationUnit) element; IType[] types = cu.getTypes(); for (IType type : types) { if (!addedTypes.contains(type)) { allTypes.add(type); } } } } } } } } } } catch (Exception e1) { ArquillianUIActivator.log(e1); } } }
From source file:org.jboss.tools.arquillian.ui.internal.utils.ArquillianUIUtil.java
License:Open Source License
private static IFile getNewFile(IJavaProject javaProject, String arquillianProperties) throws CoreException { IProject project = javaProject.getProject(); if (project.hasNature(IMavenConstants.NATURE_ID)) { IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project, new NullProgressMonitor()); MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor()); Build build = mavenProject.getBuild(); String testDirectory = null; List<Resource> testResources = build.getTestResources(); if (testResources != null && testResources.size() > 0) { testDirectory = testResources.get(0).getDirectory(); } else {/*www .ja va2s.co m*/ testDirectory = build.getTestSourceDirectory(); } File testDir = new File(testDirectory); if (testDir.isDirectory()) { File arquillianFile = new File(testDir, arquillianProperties); IPath path = new Path(arquillianFile.getAbsolutePath()); IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); if (!iFile.getParent().exists()) { IPath projectPath = javaProject.getProject().getLocation(); IPath iFilePath = iFile.getLocation(); if (iFilePath.toString().startsWith(projectPath.toString())) { String s = iFilePath.toString().substring(projectPath.toString().length()); path = new Path(s); return javaProject.getProject().getFile(path); } } return iFile; } } IPath path = null; IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); for (IClasspathEntry entry : rawClasspath) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry); if (roots == null) { continue; } for (IPackageFragmentRoot root : roots) { path = root.getPath(); break; } } } if (path == null) { throw new CoreException(new Status(IStatus.ERROR, ArquillianUIActivator.PLUGIN_ID, "Invalid project")); } IFolder folder = javaProject.getProject().getFolder(path); if (!folder.exists()) { IPath projectPath = javaProject.getPath(); path = path.makeRelativeTo(projectPath); folder = javaProject.getProject().getFolder(path); } return folder.getFile(arquillianProperties); }
From source file:org.jboss.tools.maven.apt.AptProjectConfigurator.java
License:Open Source License
/** * {@inheritDoc}//from w w w. j a v a 2 s . c om */ public void configureRawClasspath(ProjectConfigurationRequest request, IClasspathDescriptor classpath, IProgressMonitor monitor) throws CoreException { /* * We need to prevent/recover from the JavaProjectConfigurator removing the * generated annotation sources directory from the classpath: it will be added * when we configure the Eclipse APT preferences and then removed when the * JavaProjectConfigurator runs. */ // Get the various project references we'll need IProject eclipseProject = request.getProject(); MavenProject mavenProject = request.getMavenProject(); IMavenProjectFacade projectFacade = request.getMavenProjectFacade(); // If this isn't a Java project, we have nothing to do if (!eclipseProject.hasNature(JavaCore.NATURE_ID)) return; //If APT is not enabled, nothing to do either IJavaProject javaProject = JavaCore.create(eclipseProject); if (!AptConfig.isEnabled(javaProject)) { return; } // If this project has no valid compiler plugin config, we have nothing to do File generatedSourcesDirectory = getGeneratedSourcesDirectory(request.getMavenSession(), projectFacade, monitor); if (generatedSourcesDirectory == null) return; // Get the generated annotation sources directory as an IFolder File generatedSourcesRelativeDirectory = convertToProjectRelativePath(eclipseProject, generatedSourcesDirectory); String generatedSourcesRelativeDirectoryPath = generatedSourcesRelativeDirectory.getPath(); IFolder generatedSourcesFolder = eclipseProject.getFolder(generatedSourcesRelativeDirectoryPath); // Get the output folder to use as an IPath File outputFile = new File(mavenProject.getBuild().getOutputDirectory()); File outputRelativeFile = convertToProjectRelativePath(eclipseProject, outputFile); IFolder outputFolder = eclipseProject.getFolder(outputRelativeFile.getPath()); IPath outputPath = outputFolder.getFullPath(); // Create the includes & excludes specifiers IPath[] includes = new IPath[] {}; IPath[] excludes = new IPath[] {}; // If the source folder exists and is non-nested, add it if (generatedSourcesFolder != null && generatedSourcesFolder.exists() && generatedSourcesFolder.getProject().equals(eclipseProject)) { IClasspathEntryDescriptor cped = getEnclosingEntryDescriptor(classpath, generatedSourcesFolder.getFullPath()); if (cped == null) { classpath.addSourceEntry(generatedSourcesFolder.getFullPath(), outputPath, includes, excludes, false); } } else { if (generatedSourcesFolder != null) { classpath.removeEntry(generatedSourcesFolder.getFullPath()); } } }
From source file:org.jboss.tools.maven.apt.internal.AbstractAptConfiguratorDelegate.java
License:Open Source License
public void configureClasspath(IClasspathDescriptor classpath, IProgressMonitor monitor) throws CoreException { AnnotationProcessorConfiguration configuration = getAnnotationProcessorConfiguration(monitor); if (configuration == null || !configuration.isAnnotationProcessingEnabled()) { return;//from ww w. j a v a 2s .co m } //Add generated source directory to classpath File generatedSourcesDirectory = configuration.getOutputDirectory(); MavenProject mavenProject = mavenFacade.getMavenProject(); IProject eclipseProject = mavenFacade.getProject(); if (generatedSourcesDirectory != null && generatedSourcesDirectory.exists()) { File outputFolder = new File(mavenProject.getBuild().getOutputDirectory()); addToClassPath(eclipseProject, generatedSourcesDirectory, outputFolder, classpath); } //Add generated test source directory to classpath File generatedTestSourcesDirectory = configuration.getTestOutputDirectory(); if (generatedTestSourcesDirectory != null && generatedTestSourcesDirectory.exists()) { File outputFolder = new File(mavenProject.getBuild().getTestOutputDirectory()); addToClassPath(eclipseProject, generatedTestSourcesDirectory, outputFolder, classpath); } }
From source file:org.jboss.tools.maven.polyglot.poc.internal.core.PomTranslatorJob.java
License:Open Source License
private void translatePom(IFile input, IProgressMonitor monitor) throws CoreException { markerManager.deleteMarkers(input, TRANSLATION_PROBLEM_TYPE); IProject project = input.getProject(); IFile pomXml = project.getFile(IMavenConstants.POM_FILE_NAME); IMavenProjectFacade facade = projectManager.create(pomXml, true, monitor); MavenProject mavenProject = facade.getMavenProject(monitor); IPath polyglotFolder = facade.getProjectRelativePath(mavenProject.getBuild().getDirectory()) .append("polyglot"); IFile output = project.getFolder(polyglotFolder).getFile(IMavenConstants.POM_FILE_NAME); MavenExecutionResult result = translate(pomXml, input, output, monitor); if (result.hasExceptions()) { addErrorMarkers(input, result.getExceptions()); return;/* ww w . j a v a2s . c o m*/ } if (output.exists()) { pomXml.setContents(output.getContents(), true, true, monitor); if (!pomXml.isDerived()) { pomXml.setDerived(true, monitor); } } }
From source file:org.jenkinsci.plugins.maveninvoker.MavenInvokerArchiver.java
License:Apache License
@Override public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener, Throwable error) throws InterruptedException, IOException { if (!mojo.is("org.apache.maven.plugins", "maven-invoker-plugin", "run") && !mojo.is("org.apache.maven.plugins", "maven-invoker-plugin", "integration-test")) { return true; }//from www .j ava2 s . co m final String buildDirectory = new File(pom.getBuild().getDirectory()).getName(); final PrintStream logger = listener.getLogger(); logger.println("MavenInvokerArchiver"); File[] reports = null; try { // projectsDirectory final File projectsDirectory = mojo.getConfigurationValue("projectsDirectory", File.class); // cloneProjectsTo final File cloneProjectsTo = mojo.getConfigurationValue("cloneProjectsTo", File.class); final File reportsDir = mojo.getConfigurationValue("reportsDirectory", File.class); if (reportsDir != null) { reports = reportsDir.listFiles(new FilenameFilter() { @Override public boolean accept(File file, String s) { return s.startsWith("BUILD"); } }); } if (reports != null) { logger.println("found reports:" + Arrays.asList(reports)); } else { logger.println("no reports found"); return true; } final BuildJobXpp3Reader reader = new BuildJobXpp3Reader(); final MavenInvokerResults mavenInvokerResults = new MavenInvokerResults(); // TODO // saveReports for (File f : reports) { InputStream is = new FileInputStream(f); try { BuildJob buildJob = reader.read(is); MavenInvokerResult mavenInvokerResult = MavenInvokerRecorder.map(buildJob); mavenInvokerResult.mavenModuleName = pom.getArtifactId(); mavenInvokerResults.mavenInvokerResults.add(mavenInvokerResult); } catch (XmlPullParserException e) { e.printStackTrace(listener.fatalError("failed to parse report")); build.setResult(Result.FAILURE); return true; } finally { IOUtils.closeQuietly(is); } } logger.println("Finished parsing Maven Invoker results"); int failedCount = build.execute(new MavenBuildProxy.BuildCallable<Integer, IOException>() { private static final long serialVersionUID = 1L; @Override public Integer call(MavenBuild aBuild) throws IOException, IOException, InterruptedException { if (reportsDir == null) { return 0; } FilePath[] reportsPaths = MavenInvokerRecorder.locateReports(aBuild.getWorkspace(), buildDirectory + "/" + reportsDir.getName() + "/BUILD*.xml"); FilePath backupDirectory = MavenInvokerRecorder.getMavenInvokerReportsDirectory(aBuild); MavenInvokerRecorder.saveReports(backupDirectory, reportsPaths); List<FilePath> allBuildLogs = new ArrayList<FilePath>(); for (MavenInvokerResult mavenInvokerResult : mavenInvokerResults.mavenInvokerResults) { // search build.log files File invokerBuildDir = cloneProjectsTo == null ? projectsDirectory : cloneProjectsTo; File projectDir = new File(invokerBuildDir, mavenInvokerResult.project); FilePath[] buildLogs = null; File parentFile = projectDir.getParentFile(); if (parentFile != null) { buildLogs = MavenInvokerRecorder.locateBuildLogs(aBuild.getWorkspace(), "**/" + parentFile.getName()); } if (buildLogs != null) { allBuildLogs.addAll(Arrays.asList(buildLogs)); } } // backup all build.log MavenInvokerRecorder.saveBuildLogs(backupDirectory, allBuildLogs); InvokerReport invokerReport = new InvokerReport(aBuild, mavenInvokerResults); aBuild.addAction(invokerReport); int failed = invokerReport.getFailedTestCount(); return failed; } }); return true; } catch (ComponentConfigurationException e) { e.printStackTrace(listener.fatalError("failed to find report directory")); build.setResult(Result.FAILURE); return true; } }
From source file:org.jenkinsci.plugins.pipeline.maven.eventspy.handler.AbstractMavenEventHandler.java
License:Open Source License
public Xpp3Dom newElement(@Nonnull String name, @Nullable final MavenProject project) { Xpp3Dom projectElt = new Xpp3Dom(name); if (project == null) { return projectElt; }/*from w ww. j a va 2 s . c om*/ projectElt.setAttribute("name", project.getName()); projectElt.setAttribute("groupId", project.getGroupId()); projectElt.setAttribute("artifactId", project.getArtifactId()); projectElt.setAttribute("version", project.getVersion()); projectElt.setAttribute("packaging", project.getPackaging()); if (project.getBasedir() != null) { try { projectElt.setAttribute("baseDir", project.getBasedir().getCanonicalPath()); } catch (IOException e) { throw new RuntimeIOException(e); } } if (project.getFile() != null) { File projectFile = project.getFile(); String absolutePath; try { absolutePath = projectFile.getCanonicalPath(); } catch (IOException e) { throw new RuntimeIOException(e); } if (absolutePath.endsWith(File.separator + "pom.xml") || absolutePath.endsWith(File.separator + ".flattened-pom.xml")) { // JENKINS-43616: flatten-maven-plugin replaces the original pom as artifact with a .flattened-pom.xml // no tweak } else if (absolutePath.endsWith(File.separator + "dependency-reduced-pom.xml")) { // JENKINS-42302: maven-shade-plugin creates a temporary project file dependency-reduced-pom.xml // TODO see if there is a better way to implement this "workaround" absolutePath = absolutePath.replace(File.separator + "dependency-reduced-pom.xml", File.separator + "pom.xml"); } else if (absolutePath.endsWith(File.separator + ".git-versioned-pom.xml")) { // JENKINS-56666 maven-git-versioning-extension causes warnings due to temporary pom.xml file name '.git-versioned-pom.xml' // https://github.com/qoomon/maven-git-versioning-extension/blob/v4.1.0/src/main/java/me/qoomon/maven/gitversioning/VersioningMojo.java#L39 // TODO see if there is a better way to implement this "workaround" absolutePath = absolutePath.replace(File.separator + ".git-versioned-pom.xml", File.separator + "pom.xml"); } else { String flattenedPomFilename = getMavenFlattenPluginFlattenedPomFilename(project); if (flattenedPomFilename == null) { logger.warn("[jenkins-event-spy] Unexpected Maven project file name '" + projectFile.getName() + "', problems may occur"); } else { if (absolutePath.endsWith(File.separator + flattenedPomFilename)) { absolutePath = absolutePath.replace(File.separator + flattenedPomFilename, File.separator + "pom.xml"); } else { logger.warn("[jenkins-event-spy] Unexpected Maven project file name '" + projectFile.getName() + "', problems may occur"); } } } projectElt.setAttribute("file", absolutePath); } Build build = project.getBuild(); if (build != null) { Xpp3Dom buildElt = new Xpp3Dom("build"); projectElt.addChild(buildElt); if (build.getOutputDirectory() != null) { buildElt.setAttribute("directory", build.getDirectory()); } if (build.getSourceDirectory() != null) { buildElt.setAttribute("sourceDirectory", build.getSourceDirectory()); } } return projectElt; }
From source file:org.jetbrains.idea.maven.server.Maven30ServerEmbedderImpl.java
License:Apache License
@Override public Collection<MavenArtifact> resolvePlugin(@Nonnull final MavenPlugin plugin, @Nonnull final List<MavenRemoteRepository> repositories, int nativeMavenProjectId, final boolean transitive) throws RemoteException, MavenServerProcessCanceledException { try {//from w ww . j a va 2 s .c om Plugin mavenPlugin = new Plugin(); mavenPlugin.setGroupId(plugin.getGroupId()); mavenPlugin.setArtifactId(plugin.getArtifactId()); mavenPlugin.setVersion(plugin.getVersion()); MavenProject project = RemoteNativeMavenProjectHolder.findProjectById(nativeMavenProjectId); Plugin pluginFromProject = project.getBuild().getPluginsAsMap() .get(plugin.getGroupId() + ':' + plugin.getArtifactId()); if (pluginFromProject != null) { mavenPlugin.setDependencies(pluginFromProject.getDependencies()); } final MavenExecutionRequest request = createRequest(null, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList()); DefaultMaven maven = (DefaultMaven) getComponent(Maven.class); RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request); PluginDependenciesResolver pluginDependenciesResolver = getComponent(PluginDependenciesResolver.class); org.sonatype.aether.artifact.Artifact pluginArtifact = pluginDependenciesResolver.resolve(mavenPlugin, project.getRemotePluginRepositories(), repositorySystemSession); org.sonatype.aether.graph.DependencyNode node = pluginDependenciesResolver.resolve(mavenPlugin, pluginArtifact, null, project.getRemotePluginRepositories(), repositorySystemSession); PreorderNodeListGenerator nlg = new PreorderNodeListGenerator(); node.accept(nlg); List<MavenArtifact> res = new ArrayList<MavenArtifact>(); for (org.sonatype.aether.artifact.Artifact artifact : nlg.getArtifacts(true)) { if (!MavenStringUtil.equal(artifact.getArtifactId(), plugin.getArtifactId()) || !MavenStringUtil.equal(artifact.getGroupId(), plugin.getGroupId())) { res.add(MavenModelConverter.convertArtifact(RepositoryUtils.toArtifact(artifact), getLocalRepositoryFile())); } } return res; } catch (Exception e) { Maven3ServerGlobals.getLogger().info(e); return Collections.emptyList(); } }
From source file:org.jetbrains.idea.maven.server.Maven32ServerEmbedderImpl.java
License:Apache License
@Override public Collection<MavenArtifact> resolvePlugin(@Nonnull final MavenPlugin plugin, @Nonnull final List<MavenRemoteRepository> repositories, int nativeMavenProjectId, final boolean transitive) throws RemoteException, MavenServerProcessCanceledException { try {/* w w w . j a v a2 s. co m*/ Plugin mavenPlugin = new Plugin(); mavenPlugin.setGroupId(plugin.getGroupId()); mavenPlugin.setArtifactId(plugin.getArtifactId()); mavenPlugin.setVersion(plugin.getVersion()); MavenProject project = RemoteNativeMavenProjectHolder.findProjectById(nativeMavenProjectId); Plugin pluginFromProject = project.getBuild().getPluginsAsMap() .get(plugin.getGroupId() + ':' + plugin.getArtifactId()); if (pluginFromProject != null) { mavenPlugin.setDependencies(pluginFromProject.getDependencies()); } final MavenExecutionRequest request = createRequest(null, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList()); DefaultMaven maven = (DefaultMaven) getComponent(Maven.class); RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request); PluginDependenciesResolver pluginDependenciesResolver = getComponent(PluginDependenciesResolver.class); org.eclipse.aether.artifact.Artifact pluginArtifact = pluginDependenciesResolver.resolve(mavenPlugin, project.getRemotePluginRepositories(), repositorySystemSession); org.eclipse.aether.graph.DependencyNode node = pluginDependenciesResolver.resolve(mavenPlugin, pluginArtifact, null, project.getRemotePluginRepositories(), repositorySystemSession); PreorderNodeListGenerator nlg = new PreorderNodeListGenerator(); node.accept(nlg); List<MavenArtifact> res = new ArrayList<MavenArtifact>(); for (org.eclipse.aether.artifact.Artifact artifact : nlg.getArtifacts(true)) { if (!MavenStringUtil.equal(artifact.getArtifactId(), plugin.getArtifactId()) || !MavenStringUtil.equal(artifact.getGroupId(), plugin.getGroupId())) { res.add(MavenModelConverter.convertArtifact(RepositoryUtils.toArtifact(artifact), getLocalRepositoryFile())); } } return res; } catch (Exception e) { Maven3ServerGlobals.getLogger().info(e); return Collections.emptyList(); } }