All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.plugin.eclipse.EclipsePlugin Maven / Gradle / Ivy

Go to download

The Eclipse Plugin is used to generate Eclipse IDE files (.project, .classpath and the .settings folder) from a POM.

There is a newer version: 2.10
Show newest version
package org.apache.maven.plugin.eclipse;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed 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.
 */

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.eclipse.writers.EclipseClasspathWriter;
import org.apache.maven.plugin.eclipse.writers.EclipseProjectWriter;
import org.apache.maven.plugin.eclipse.writers.EclipseSettingsWriter;
import org.apache.maven.plugin.eclipse.writers.EclipseWtpComponentWriter;
import org.apache.maven.plugin.eclipse.writers.EclipseWtpFacetsWriter;
import org.apache.maven.plugin.eclipse.writers.EclipseWtpmodulesWriter;
import org.apache.maven.plugin.ide.AbstractIdeSupportMojo;
import org.apache.maven.plugin.ide.IdeDependency;
import org.apache.maven.plugin.ide.IdeUtils;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;

/**
 * Generates the following eclipse configuration files:
 * 
    *
  • .project and .classpath files
  • *
  • .setting/org.eclipse.jdt.core.prefs with project specific compiler settings
  • *
  • various configuration files for WTP (Web Tools Project), if the parameter wtpversion is set to a * valid version (WTP configuration is not generated by default)
  • *
* If this goal is run on a multiproject root, dependencies between modules will be configured as direct project * dependencies in Eclipse (unless useProjectReferences is set to false) * * @author Trygve Laugstøl * @author Fabrizio Giustina * @version $Id: EclipsePlugin.java 391954 2006-04-06 10:21:29Z fgiust $ * @goal eclipse * @execute phase="generate-resources" */ public class EclipsePlugin extends AbstractIdeSupportMojo { private static final String NATURE_WST_FACET_CORE_NATURE = "org.eclipse.wst.common.project.facet.core.nature"; //$NON-NLS-1$ private static final String BUILDER_WST_COMPONENT_STRUCTURAL_DEPENDENCY_RESOLVER = "org.eclipse.wst.common.modulecore.ComponentStructuralBuilderDependencyResolver"; //$NON-NLS-1$ private static final String BUILDER_WST_VALIDATION = "org.eclipse.wst.validation.validationbuilder"; //$NON-NLS-1$ private static final String BUILDER_JDT_CORE_JAVA = "org.eclipse.jdt.core.javabuilder"; //$NON-NLS-1$ private static final String BUILDER_WST_COMPONENT_STRUCTURAL = "org.eclipse.wst.common.modulecore.ComponentStructuralBuilder"; //$NON-NLS-1$ private static final String NATURE_WST_MODULE_CORE_NATURE = "org.eclipse.wst.common.modulecore.ModuleCoreNature"; //$NON-NLS-1$ private static final String NATURE_JDT_CORE_JAVA = "org.eclipse.jdt.core.javanature"; //$NON-NLS-1$ private static final String NATURE_JEM_WORKBENCH_JAVA_EMF = "org.eclipse.jem.workbench.JavaEMFNature"; //$NON-NLS-1$ private static final String COMMON_PATH_JDT_LAUNCHING_JRE_CONTAINER = "org.eclipse.jdt.launching.JRE_CONTAINER"; //$NON-NLS-1$ // warning, order is important for binary search public static final String[] WTP_SUPPORTED_VERSIONS = new String[] { "1.0", "R7", "none" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ /** * Constant for 'artifactId' element in POM.xml. */ private static final String POM_ELT_ARTIFACT_ID = "artifactId"; //$NON-NLS-1$ /** * Constant for 'groupId' element in POM.xml. */ private static final String POM_ELT_GROUP_ID = "groupId"; //$NON-NLS-1$ /** * List of eclipse project natures. By default the * org.eclipse.jdt.core.javanature nature plus the needed WTP * natures are added. Natures added using this property replace the default list. * *
     * <projectnatures>
     *    <projectnature>org.eclipse.jdt.core.javanature</projectnature>
     *    <projectnature>org.eclipse.wst.common.modulecore.ModuleCoreNature</projectnature>
     * </projectnatures>
     * 
* * @parameter */ private List projectnatures; /** * List of eclipse project natures to be added to the default ones. * *
     * <additionalProjectnatures>
     *    <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
     * </additionalProjectnatures>
     * 
* * @parameter */ private List additionalProjectnatures; /** * List of eclipse build commands. By default the org.eclipse.jdt.core.javabuilder builder plus the needed * WTP builders are added. Configuration example: * *
     * <buildcommands>
     *    <buildcommand>org.eclipse.wst.common.modulecore.ComponentStructuralBuilder</buildcommand>
     *    <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
     *    <buildcommand>org.eclipse.wst.common.modulecore.ComponentStructuralBuilderDependencyResolver</buildcommand>
     * </buildcommands>
     * 
* * @parameter */ private List buildcommands; /** * List of eclipse build commands to be added to the default ones. * *
     * <additionalBuildcommands>
     *    <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
     * </additionalBuildcommands>
     * 
* * @parameter */ private List additionalBuildcommands; /** * List of container classpath entries. By default the org.eclipse.jdt.launching.JRE_CONTAINER classpath * container is added. Configuration example: *
     * <classpathContainers>
     *    <buildcommand>org.eclipse.jdt.launching.JRE_CONTAINER</buildcommand>
     *    <buildcommand>org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v5.5</buildcommand>
     *    <buildcommand>org.eclipse.jst.j2ee.internal.web.container/artifact</buildcommand>
     * </classpathContainers>
     * 
* * @parameter */ private List classpathContainers; /** * @parameter expression="${eclipse.downloadSources}" * @deprecated use downloadSources */ private boolean eclipseDownloadSources; /** * Eclipse workspace directory. * * @parameter expression="${eclipse.workspace}" alias="outputDir" */ private File eclipseProjectDir; /** * When set to false, the plugin will not create sub-projects and instead * reference those sub-projects using the installed package in the local * repository * * @parameter expression="${eclipse.useProjectReferences}" default-value="true" * @required */ private boolean useProjectReferences; /** * The default output directory * * @parameter expression="${outputDirectory}" alias="outputDirectory" default-value="${project.build.outputDirectory}" * @required */ private File buildOutputDirectory; /** * The version of WTP for which configuration files will be generated. * The default value is "none" (don't generate WTP configuration), supported versions are "R7" and "1.0" * * @parameter expression="${wtpversion}" default-value="none" */ private String wtpversion; /** * Not a plugin parameter. Are we working with wtp r7? */ private boolean wtpR7; /** * Not a plugin parameter. Are we working with wtp 1.0? */ private boolean wtp10; /** * Not a plugin parameter. Is this a java project? */ private boolean isJavaProject; /** * Getter for buildcommands. * @return Returns the buildcommands. */ public List getBuildcommands() { return this.buildcommands; } /** * Setter for buildcommands. * @param buildcommands The buildcommands to set. */ public void setBuildcommands( List buildcommands ) { this.buildcommands = buildcommands; } /** * Getter for buildOutputDirectory. * @return Returns the buildOutputDirectory. */ public File getBuildOutputDirectory() { return this.buildOutputDirectory; } /** * Setter for buildOutputDirectory. * @param buildOutputDirectory The buildOutputDirectory to set. */ public void setBuildOutputDirectory( File buildOutputDirectory ) { this.buildOutputDirectory = buildOutputDirectory; } /** * Getter for classpathContainers. * @return Returns the classpathContainers. */ public List getClasspathContainers() { return this.classpathContainers; } /** * Setter for classpathContainers. * @param classpathContainers The classpathContainers to set. */ public void setClasspathContainers( List classpathContainers ) { this.classpathContainers = classpathContainers; } /** * Getter for eclipseProjectDir. * @return Returns the eclipseProjectDir. */ public File getEclipseProjectDir() { return this.eclipseProjectDir; } /** * Setter for eclipseProjectDir. * @param eclipseProjectDir The eclipseProjectDir to set. */ public void setEclipseProjectDir( File eclipseProjectDir ) { this.eclipseProjectDir = eclipseProjectDir; } /** * Getter for projectnatures. * @return Returns the projectnatures. */ public List getProjectnatures() { return this.projectnatures; } /** * Setter for projectnatures. * @param projectnatures The projectnatures to set. */ public void setProjectnatures( List projectnatures ) { this.projectnatures = projectnatures; } /** * Getter for useProjectReferences. * @return Returns the useProjectReferences. */ public boolean getUseProjectReferences() { return this.useProjectReferences; } /** * Setter for useProjectReferences. * @param useProjectReferences The useProjectReferences to set. */ public void setUseProjectReferences( boolean useProjectReferences ) { this.useProjectReferences = useProjectReferences; } /** * Getter for wtpversion. * @return Returns the wtpversion. */ public String getWtpversion() { return this.wtpversion; } /** * Setter for wtpversion. * @param wtpversion The wtpversion to set. */ public void setWtpversion( String wtpversion ) { this.wtpversion = wtpversion; } /** * Getter for additionalBuildcommands. * @return Returns the additionalBuildcommands. */ public List getAdditionalBuildcommands() { return this.additionalBuildcommands; } /** * Setter for additionalBuildcommands. * @param additionalBuildcommands The additionalBuildcommands to set. */ public void setAdditionalBuildcommands( List additionalBuildcommands ) { this.additionalBuildcommands = additionalBuildcommands; } /** * Getter for additionalProjectnatures. * @return Returns the additionalProjectnatures. */ public List getAdditionalProjectnatures() { return this.additionalProjectnatures; } /** * Setter for additionalProjectnatures. * @param additionalProjectnatures The additionalProjectnatures to set. */ public void setAdditionalProjectnatures( List additionalProjectnatures ) { this.additionalProjectnatures = additionalProjectnatures; } /** * @see org.apache.maven.plugin.Mojo#execute() */ public boolean setup() throws MojoExecutionException { if ( eclipseDownloadSources ) { // deprecated warning getLog().warn( Messages.getString( "EclipsePlugin.deprecatedpar", new Object[] { //$NON-NLS-1$ "eclipse.downloadSources", //$NON-NLS-1$ "downloadSources" } ) ); //$NON-NLS-1$ downloadSources = true; } if ( Arrays.binarySearch( WTP_SUPPORTED_VERSIONS, wtpversion ) < 0 ) { throw new MojoExecutionException( Messages .getString( "EclipsePlugin.unsupportedwtp", new Object[] { //$NON-NLS-1$ wtpversion, StringUtils.join( WTP_SUPPORTED_VERSIONS, " " ) } ) ); //$NON-NLS-1$ } if ( "R7".equalsIgnoreCase( wtpversion ) ) //$NON-NLS-1$ { wtpR7 = true; } else if ( "1.0".equalsIgnoreCase( wtpversion ) ) //$NON-NLS-1$ { wtp10 = true; } if ( !"none".equalsIgnoreCase( wtpversion ) ) { getLog().info( Messages.getString( "EclipsePlugin.wtpversion", wtpversion ) ); } String packaging = executedProject.getPackaging(); // validate sanity of the current m2 project assertNotEmpty( executedProject.getGroupId(), POM_ELT_GROUP_ID ); //$NON-NLS-1$ assertNotEmpty( executedProject.getArtifactId(), POM_ELT_ARTIFACT_ID ); //$NON-NLS-1$ if ( executedProject.getFile() == null || !executedProject.getFile().exists() ) { throw new MojoExecutionException( Messages.getString( "EclipsePlugin.missingpom" ) ); //$NON-NLS-1$ } if ( "pom".equals( packaging ) && eclipseProjectDir == null ) //$NON-NLS-1$ { getLog().info( Messages.getString( "EclipsePlugin.pompackaging" ) ); //$NON-NLS-1$ return false; } if ( eclipseProjectDir == null ) { eclipseProjectDir = executedProject.getFile().getParentFile(); } else if ( !eclipseProjectDir.equals( executedProject.getFile().getParentFile() ) ) { if ( !eclipseProjectDir.isDirectory() ) { throw new MojoExecutionException( Messages.getString( "EclipsePlugin.notadir", eclipseProjectDir ) ); //$NON-NLS-1$ } eclipseProjectDir = new File( eclipseProjectDir, executedProject.getArtifactId() ); if ( !eclipseProjectDir.isDirectory() && !eclipseProjectDir.mkdirs() ) { throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantcreatedir", eclipseProjectDir ) ); //$NON-NLS-1$ } } // end validate ArtifactHandler artifactHandler = this.project.getArtifact().getArtifactHandler(); // ear projects don't contain java sources isJavaProject = "java".equals( artifactHandler.getLanguage() ) && !"ear".equals( packaging ); // defaults if ( projectnatures == null ) { fillDefaultNatures( packaging ); } if ( additionalProjectnatures != null ) { projectnatures.addAll( additionalProjectnatures ); } if ( buildcommands == null ) { fillDefaultBuilders( packaging ); } if ( additionalBuildcommands != null ) { buildcommands.addAll( additionalBuildcommands ); } if ( classpathContainers == null ) { fillDefaultClasspathContainers( packaging ); } else if ( !classpathContainers.contains( COMMON_PATH_JDT_LAUNCHING_JRE_CONTAINER ) ) //$NON-NLS-1$ { getLog().warn( Messages.getString( "EclipsePlugin.missingjrecontainer" ) ); //$NON-NLS-1$ classpathContainers.add( 0, COMMON_PATH_JDT_LAUNCHING_JRE_CONTAINER ); } // ready to start return true; } public void writeConfiguration( IdeDependency[] deps ) throws MojoExecutionException { File projectBaseDir = executedProject.getFile().getParentFile(); // build a list of UNIQUE source dirs (both src and resources) to be // used in classpath and wtpmodules EclipseSourceDir[] sourceDirs = buildDirectoryList( executedProject, eclipseProjectDir, buildOutputDirectory ); if ( wtpR7 ) { new EclipseWtpmodulesWriter( getLog(), eclipseProjectDir, project, deps ).write( sourceDirs, localRepository, buildOutputDirectory ); } else if ( wtp10 ) { new EclipseWtpFacetsWriter( getLog(), eclipseProjectDir, project, deps ).write( sourceDirs, localRepository, buildOutputDirectory ); new EclipseWtpComponentWriter( getLog(), eclipseProjectDir, project, deps ).write( sourceDirs, localRepository, buildOutputDirectory ); } new EclipseProjectWriter( getLog(), eclipseProjectDir, project, deps ).write( projectBaseDir, executedProject, projectnatures, buildcommands ); new EclipseSettingsWriter( getLog(), eclipseProjectDir, project, deps ).write(); if ( isJavaProject ) { new EclipseClasspathWriter( getLog(), eclipseProjectDir, project, deps ).write( projectBaseDir, sourceDirs, classpathContainers, localRepository, buildOutputDirectory ); } getLog().info( Messages.getString( "EclipsePlugin.wrote", new Object[] { //$NON-NLS-1$ project.getArtifactId(), eclipseProjectDir.getAbsolutePath() } ) ); } private void assertNotEmpty( String string, String elementName ) throws MojoExecutionException { if ( string == null ) { throw new MojoExecutionException( Messages.getString( "EclipsePlugin.missingelement", elementName ) ); //$NON-NLS-1$ } } private void fillDefaultNatures( String packaging ) { projectnatures = new ArrayList(); if ( wtp10 ) { projectnatures.add( NATURE_WST_FACET_CORE_NATURE ); // WTP 1.0 nature } if ( isJavaProject ) { projectnatures.add( NATURE_JDT_CORE_JAVA ); } if ( wtpR7 || wtp10 ) { projectnatures.add( NATURE_WST_MODULE_CORE_NATURE ); // WTP 0.7/1.0 nature if ( isJavaProject ) { projectnatures.add( NATURE_JEM_WORKBENCH_JAVA_EMF ); // WTP 0.7/1.0 nature } } } private void fillDefaultClasspathContainers( String packaging ) { classpathContainers = new ArrayList(); classpathContainers.add( COMMON_PATH_JDT_LAUNCHING_JRE_CONTAINER ); } private void fillDefaultBuilders( String packaging ) { buildcommands = new ArrayList(); if ( wtpR7 ) { buildcommands.add( BUILDER_WST_COMPONENT_STRUCTURAL ); // WTP 0.7 builder } if ( isJavaProject ) { buildcommands.add( BUILDER_JDT_CORE_JAVA ); } if ( wtpR7 || wtp10 ) { buildcommands.add( BUILDER_WST_VALIDATION ); // WTP 0.7/1.0 builder } if ( wtpR7 ) { buildcommands.add( BUILDER_WST_COMPONENT_STRUCTURAL_DEPENDENCY_RESOLVER ); // WTP 0.7 builder } } public EclipseSourceDir[] buildDirectoryList( MavenProject project, File basedir, File buildOutputDirectory ) throws MojoExecutionException { File projectBaseDir = project.getFile().getParentFile(); // avoid duplicated entries Set directories = new TreeSet(); extractSourceDirs( directories, project.getCompileSourceRoots(), basedir, projectBaseDir, false, null ); String relativeOutput = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, buildOutputDirectory, false ); extractResourceDirs( directories, project.getBuild().getResources(), project, basedir, projectBaseDir, false, relativeOutput ); // If using the standard output location, don't mix the test output into it. String testOutput = null; boolean useFixedOutputDir = !buildOutputDirectory.equals( new File( project.getBuild().getOutputDirectory() ) ); if ( !useFixedOutputDir ) { testOutput = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, new File( project.getBuild() .getTestOutputDirectory() ), false ); } extractSourceDirs( directories, project.getTestCompileSourceRoots(), basedir, projectBaseDir, true, testOutput ); extractResourceDirs( directories, project.getBuild().getTestResources(), project, basedir, projectBaseDir, true, testOutput ); return (EclipseSourceDir[]) directories.toArray( new EclipseSourceDir[directories.size()] ); } private static void extractSourceDirs( Set directories, List sourceRoots, File basedir, File projectBaseDir, boolean test, String output ) throws MojoExecutionException { for ( Iterator it = sourceRoots.iterator(); it.hasNext(); ) { File sourceRootFile = new File( (String) it.next() ); if ( sourceRootFile.isDirectory() ) { String sourceRoot = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, sourceRootFile, !projectBaseDir .equals( basedir ) ); directories.add( new EclipseSourceDir( sourceRoot, output, test, null, null ) ); } } } private void extractResourceDirs( Set directories, List resources, MavenProject project, File basedir, File projectBaseDir, boolean test, String output ) throws MojoExecutionException { for ( Iterator it = resources.iterator(); it.hasNext(); ) { Resource resource = (Resource) it.next(); String includePattern = null; String excludePattern = null; if ( resource.getIncludes().size() != 0 ) { // @todo includePattern = ? getLog().warn( Messages.getString( "EclipsePlugin.includenotsupported" ) ); //$NON-NLS-1$ } if ( resource.getExcludes().size() != 0 ) { // @todo excludePattern = ? getLog().warn( Messages.getString( "EclipsePlugin.excludenotsupported" ) ); //$NON-NLS-1$ } // Example of setting include/exclude patterns for future reference. // // TODO: figure out how to merge if the same dir is specified twice // with different in/exclude patterns. We can't write them now, // since only the the first one would be included. // // if ( resource.getIncludes().size() != 0 ) // { // writer.addAttribute( // "including", StringUtils.join( resource.getIncludes().iterator(), // "|" ) // ); // } // // if ( resource.getExcludes().size() != 0 ) // { // writer.addAttribute( // "excluding", StringUtils.join( resource.getExcludes().iterator(), // "|" ) // ); // } File resourceDirectory = new File( resource.getDirectory() ); if ( !resourceDirectory.exists() || !resourceDirectory.isDirectory() ) { continue; } String resourceDir = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, resourceDirectory, !projectBaseDir .equals( basedir ) ); if ( output != null ) { File outputFile = new File( projectBaseDir, output ); // create output dir if it doesn't exist outputFile.mkdirs(); if ( !StringUtils.isEmpty( resource.getTargetPath() ) ) { outputFile = new File( outputFile, resource.getTargetPath() ); // create output dir if it doesn't exist outputFile.mkdirs(); } output = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, outputFile, false ); } directories.add( new EclipseSourceDir( resourceDir, output, test, includePattern, excludePattern ) ); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy