### Eclipse Workspace Patch 1.0 #P maven-eclipse-plugin-2.8 Index: src/main/java/org/apache/maven/plugin/ide/AbstractIdeSupportMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/ide/AbstractIdeSupportMojo.java (revision 1173849) +++ src/main/java/org/apache/maven/plugin/ide/AbstractIdeSupportMojo.java (working copy) @@ -220,6 +220,15 @@ */ protected Logger logger; + /** + * If true the classpath config writer will include the + * dependencies of sub-modules in the given project. + * + * @parameter expression="${includeModulesInClasspath}" + * default-value="false" + */ + private boolean includeModulesInClasspath; + /** * Getter for artifactMetadataSource. * @@ -420,6 +429,26 @@ this.downloadSources = downloadSources; } + /** + * Getter for includeModulesInClasspath. + * + * @return Returns the includeModulesInClasspath. + */ + public final boolean getIncludeModulesInClasspath() + { + return includeModulesInClasspath; + } + + /** + * Setter for includeModulesInClasspath. + * + * @param includeModulesInClasspath The includeModulesInClasspath to set. + */ + public final void setIncludeModulesInClasspath( boolean includeModulesInClasspath ) + { + this.includeModulesInClasspath = includeModulesInClasspath; + } + protected void setResolveDependencies( boolean resolveDependencies ) { this.resolveDependencies = resolveDependencies; @@ -515,6 +544,32 @@ } /** + * Returns all the dependencies of the modules for the specified project. + * + * @param project The Maven project to examine for sub-module dependencies. + * @param includeSubmodules If true, sub-modules of the + * project's modules will be examined recursively. + * @return {@link List} of all dependencies + */ + private static List getModuleDependencies(MavenProject project, + boolean includeSubmodules) { + List collectedProjects = project.getCollectedProjects(); + List dependencies = new ArrayList(); + + for (int i = 0, size = collectedProjects.size(); i < size; i++) { + MavenProject collectedProject = (MavenProject) collectedProjects.get(i); + dependencies.addAll(collectedProject.getDependencies()); + + if (includeSubmodules) { + dependencies.addAll(getModuleDependencies(collectedProject, + includeSubmodules)); + } + } + + return dependencies; + } + + /** * Resolve project dependencies. Manual resolution is needed in order to avoid resolution of multiproject artifacts * (if projects will be linked each other an installed jar is not needed) and to avoid a failure when a jar is * missing. @@ -532,7 +587,12 @@ MavenProject project = getProject(); ArtifactRepository localRepo = getLocalRepository(); - List deps = getProject().getDependencies(); + List deps = project.getDependencies(); + + // Collect dependencies from sub-modules + if ( getIncludeModulesInClasspath() ) { + deps.addAll(getModuleDependencies(project, true)); + } // Collect the list of resolved IdeDependencies. List dependencies = new ArrayList(); Index: src/main/java/org/apache/maven/plugin/eclipse/EclipsePlugin.java =================================================================== --- src/main/java/org/apache/maven/plugin/eclipse/EclipsePlugin.java (revision 1173849) +++ src/main/java/org/apache/maven/plugin/eclipse/EclipsePlugin.java (working copy) @@ -532,8 +532,33 @@ * @since 2.6.1 * @parameter */ - private List sourceIncludes; + private List sourceIncludes; + + /** + * Always create the .project configuration for Maven projects + * regardless of packaging type. + * + * Normally, the Eclipse plugin only creates Eclipse projects if the Maven + * project is a jar project or if a workspace directory is + * specified. Setting this parameter to true forces the + * creation of Eclipse project configuration even if no workspace is + * specified. + * + * @parameter expression="${eclipse.alwaysWriteProjectConfig}" + * default-value="false" + */ + private boolean alwaysWriteProjectConfig; + /** + * Forces project to be treated as if it was a Java project. Maven packaging + * type will therefore be ignored and behaviors will be as if Java. This + * includes always creating the .classpath configuration. + * + * @parameter expression="${eclipse.forceTreatAsJavaProject}" + * default-value="false" + */ + private boolean forceTreatAsJavaProject; + /** * A list of links to local files in the system. A configuration like this one in the pom : <plugin> * <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> @@ -809,6 +834,47 @@ } /** + * Getter for alwaysWriteProjectConfig. + * + * @return Returns the alwaysWriteProjectConfig. + */ + public final boolean getAlwaysWriteProjectConfig() + { + return alwaysWriteProjectConfig; + } + + /** + * Setter for alwaysWriteProjectConfig. + * + * @param alwaysWriteProjectConfig The alwaysWriteProjectConfig to set. + */ + public final void setAlwaysWriteProjectConfig( boolean alwaysWriteProjectConfig ) + { + this.alwaysWriteProjectConfig = alwaysWriteProjectConfig; + } + + /** + * Getter for forceTreatAsJavaProject. + * + * @return Returns the forceTreatAsJavaProject. + */ + public final boolean getForceTreatAsJavaProject() + { + return forceTreatAsJavaProject; + } + + /** + * Setter for forceTreatAsJavaProject. + * + * @param forceTreatAsJavaProject The forceTreatAsJavaProject to set. + */ + public final void setForceTreatAsJavaProject( boolean forceTreatAsJavaProject ) + { + this.forceTreatAsJavaProject = forceTreatAsJavaProject; + } + + + /** * @see org.apache.maven.plugin.Mojo#execute() */ public final boolean setup() @@ -832,11 +898,17 @@ pde || ( Constants.LANGUAGE_JAVA.equals( artifactHandler.getLanguage() ) && !Constants.PROJECT_PACKAGING_EAR.equals( packaging ) ); + // If the forceTreatAsJavaProject parameter is true, treat as Java project + if (!isJavaProject && getForceTreatAsJavaProject()) { + this.getLog().info( "Forcing Maven Eclipse plugin to treat as Java project." ); //$NON-NLS-1$ + isJavaProject = true; + } + if ( sourceIncludes == null ) { sourceIncludes = new ArrayList(); } - if ( isJavaProject ) + if ( isJavaProject() ) { sourceIncludes.add( JAVA_FILE_PATTERN ); } @@ -1003,8 +1075,11 @@ throw new MojoExecutionException( Messages.getString( "EclipsePlugin.missingpom" ) ); //$NON-NLS-1$ } - if ( "pom".equals( packaging ) && eclipseProjectDir == null ) //$NON-NLS-1$ + if ( "pom".equals( packaging ) && eclipseProjectDir == null && !alwaysWriteProjectConfig ) //$NON-NLS-1$ { + if (alwaysWriteProjectConfig) { + this.getLog().info( "Forcing Maven Eclipse plugin to write .project config." ); //$NON-NLS-1$ + } getLog().info( Messages.getString( "EclipsePlugin.pompackaging" ) ); //$NON-NLS-1$ return false; } @@ -1112,7 +1187,7 @@ new EclipseSettingsWriter().init( getLog(), config ).write(); - if ( isJavaProject ) + if ( isJavaProject() ) { new EclipseClasspathWriter().init( getLog(), config ).write(); if ( ajdt && ajdtVersion.equals( "1.4" ) ) @@ -1206,6 +1281,37 @@ } /** + * Returns all the source directories of the modules for the specified + * project. + * + * @param project The Maven project to examine for sub-module source dirs. + * @param includeSubmodules If true, sub-modules of the + * project's modules will be examined recursively. + * @return {@link List} of all source directories + * @throws MojoExecutionException + */ + private Set getModuleSourceDirs(MavenProject project, + boolean includeSubmodules) throws MojoExecutionException { + List collectedProjects = project.getCollectedProjects(); + LinkedHashSet sourceDirs = new LinkedHashSet(); + + for (int i = 0, size = collectedProjects.size(); i < size; i++) { + MavenProject collectedProject = (MavenProject) collectedProjects.get(i); + Set sourceDirSet = buildSourceDirectorySet( collectedProject, + this.eclipseProjectDir, this.buildOutputDirectory ); + + sourceDirs.addAll(sourceDirSet); + + if (includeSubmodules) { + sourceDirs.addAll(getModuleSourceDirs(collectedProject, + includeSubmodules)); + } + } + + return sourceDirs; + } + + /** * Create the EclipseWriterConfig for the specified dependencies. * * @param deps the project dependencies @@ -1219,8 +1325,15 @@ // build a list of UNIQUE source dirs (both src and resources) to be // used in classpath and wtpmodules - EclipseSourceDir[] sourceDirs = buildDirectoryList( executedProject, eclipseProjectDir, buildOutputDirectory ); + Set sourceDirsSet = buildSourceDirectorySet( executedProject, eclipseProjectDir, buildOutputDirectory ); + // Collect dependencies from sub-modules + if ( getIncludeModulesInClasspath() ) { + sourceDirsSet.addAll(getModuleSourceDirs(project, true)); + } + EclipseSourceDir[] sourceDirs = (EclipseSourceDir[]) sourceDirsSet + .toArray(new EclipseSourceDir[sourceDirsSet.size()]); + EclipseWriterConfig config = new EclipseWriterConfig(); config.setWorkspaceConfiguration( getWorkspaceConfiguration() ); @@ -1396,7 +1509,7 @@ projectnatures.add( NATURE_WST_FACET_CORE_NATURE ); // WTP 1.0 nature } - if ( isJavaProject ) + if ( isJavaProject() ) { if ( ajdt ) { @@ -1410,7 +1523,7 @@ { projectnatures.add( NATURE_WST_MODULE_CORE_NATURE ); // WTP 0.7/1.0 nature - if ( isJavaProject ) + if ( isJavaProject() ) { projectnatures.add( NATURE_JEM_WORKBENCH_JAVA_EMF ); // WTP 0.7/1.0 nature } @@ -1470,7 +1583,7 @@ buildcommands.add( new BuildCommand( BUILDER_WST_COMPONENT_STRUCTURAL ) ); // WTP 0.7 builder } - if ( isJavaProject ) + if ( isJavaProject() ) { if ( ajdt ) { @@ -1505,9 +1618,21 @@ } } + /** + * @deprecated @see {@link #buildSourceDirectorySet(MavenProject, File, File)} + */ public final EclipseSourceDir[] buildDirectoryList( MavenProject project, File basedir, File buildOutputDirectory ) throws MojoExecutionException { + Set sourceDirSet = buildSourceDirectorySet(project, basedir, + buildOutputDirectory); + return (EclipseSourceDir[]) sourceDirSet + .toArray(new EclipseSourceDir[sourceDirSet.size()]); + } + + public final Set buildSourceDirectorySet( MavenProject project, File basedir, File buildOutputDirectory ) + throws MojoExecutionException + { File projectBaseDir = project.getFile().getParentFile(); String mainOutput = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, buildOutputDirectory, false ); @@ -1561,7 +1686,7 @@ } if ( ajdt ) extractAspectDirs( directories, project, basedir, projectBaseDir, testOutput ); - return (EclipseSourceDir[]) directories.toArray( new EclipseSourceDir[directories.size()] ); + return directories; } private void extractSourceDirs( Set directories, List sourceRoots, File basedir, File projectBaseDir, boolean test, @@ -1576,14 +1701,36 @@ if ( sourceRootFile.isDirectory() ) { String sourceRoot = - IdeUtils.toRelativeAndFixSeparator( projectBaseDir, sourceRootFile, - !projectBaseDir.equals( basedir ) ); + IdeUtils.toRelativeAndFixSeparator( basedir, sourceRootFile, + false ); - directories.add( new EclipseSourceDir( sourceRoot, output, false, test, sourceIncludes, sourceExcludes, - false ) ); + directories.add( new EclipseSourceDir( sourceRoot, output, false, + test, filterToPath(sourceRoot, sourceIncludes), + filterToPath(sourceRoot, sourceExcludes), false ) ); } } } + + + /** + * Filters the {@link List} of itemsToFilter to only those + * items that begin with the specified path. + * + * @param path The root path to validate. + * @param itemsToFilter The unfiltered list of items. + * @return The filtered list of items. + */ + private static List filterToPath(final String path, final List itemsToFilter) { + ArrayList filtered = new ArrayList(itemsToFilter.size()); + for (int i = 0, size = itemsToFilter.size(); i < size; i++) { + String itemToFilter = (String) itemsToFilter.get(i); + + if (itemToFilter.startsWith(path) || itemToFilter.startsWith("**")) { + filtered.add(itemToFilter); + } + } + return filtered; + } final void extractResourceDirs( Set directories, List resources, File basedir, File workspaceProjectBaseDir, boolean test, final String output )