Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
Ensure that source and test folders are at the top of the classpath l…
Browse files Browse the repository at this point in the history
…ist.

After running the other Classpath Configuration classes the source folders
are out of sequence, causing all sorts of problems looking up source. This
is an attempt to re-order the classpaths.

The order of the entries are:

src/main/java
src/main/resources
src/test/java
src/test/resources

All other entries are kept the same in the classpath.  The reordering
has to be the last entry to run in the RawClasspathConfigurations.

Update reordering config to user rawclasspath

Correctly reorder the source folders after updating classpaths.
  • Loading branch information
kingargyle committed Sep 10, 2014
1 parent 696f194 commit 8ed6684
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
5 changes: 3 additions & 2 deletions me.gladwell.eclipse.m2e.android/META-INF/MANIFEST.MF
Expand Up @@ -23,5 +23,6 @@ Require-Bundle: org.eclipse.core.runtime,
org.sonatype.aether;bundle-version="1.13.1"
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Bundle-ActivationPolicy: lazy
Export-Package: me.gladwell.eclipse.m2e.android,me.gladwell.eclipse.m2
e.android.configuration
Export-Package: me.gladwell.eclipse.m2e.android,
me.gladwell.eclipse.m2e.android.configuration,
me.gladwell.eclipse.m2e.android.configuration.classpath
Expand Up @@ -24,6 +24,8 @@ List<RawClasspathConfigurer> provideRawClasspathConfigurers(PersistNonRuntimeCla
rawClasspathConfigurers.add(persistConfigurer);
rawClasspathConfigurers.add(new RemoveNonRuntimeDependenciesConfigurer());
rawClasspathConfigurers.add(removeProjectsConfigurer);
// The ReorderClasspathConfigurer needs to come last in the sequence
rawClasspathConfigurers.add(new ReorderClasspathConfigurer());

return Collections.unmodifiableList(rawClasspathConfigurers);
}
Expand Down
@@ -0,0 +1,120 @@
package me.gladwell.eclipse.m2e.android.configuration.classpath;

import java.util.ArrayList;
import java.util.LinkedHashSet;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.m2e.jdt.IClasspathDescriptor;

import me.gladwell.eclipse.m2e.android.Log;
import me.gladwell.eclipse.m2e.android.project.EclipseAndroidProject;
import me.gladwell.eclipse.m2e.android.project.MavenAndroidProject;

public class ReorderClasspathConfigurer implements RawClasspathConfigurer {

private static final String SRC_TEST_RESOURCES = "src/test/resources";
private static final String SRC_TEST_JAVA = "src/test/java";
private static final String SRC_MAIN_RESOURCES = "src/main/resources";
private static final String SRC_MAIN_JAVA = "src/main/java";
protected IClasspathEntry[] originalClasspathEntries;
protected ArrayList<IClasspathEntry> arrayEntries;

public boolean shouldApplyTo(MavenAndroidProject project) {
return true;
}

protected void addMavenSourceFolder() {
IClasspathEntry newEntry = findMavenStandardSourceFolder();
addEntry(newEntry);
}

protected void addMavenResourceFolder() {
IClasspathEntry newEntry = findMavenStandardResourceFolder();
addEntry(newEntry);
}

protected void addMavenTestSourceFolder() {
IClasspathEntry newEntry = findMavenStandardTestSourceFolder();
addEntry(newEntry);
}

protected void addMavenTestResourceFolder() {
IClasspathEntry newEntry = findMavenStandardTestResourceFolder();
addEntry(newEntry);
}

protected void addTheRest() {
for (IClasspathEntry classpathEntry : originalClasspathEntries) {
if (!classpathEntry.getPath().toString().contains(SRC_TEST_JAVA)
&& !classpathEntry.getPath().toString().contains(SRC_TEST_RESOURCES)
&& !classpathEntry.getPath().toString().contains(SRC_MAIN_RESOURCES)
&& !classpathEntry.getPath().toString().contains(SRC_MAIN_JAVA)) {
addEntry(classpathEntry);
}
}
}

protected void addEntry(IClasspathEntry entry) {
if (entry != null) {
arrayEntries.add(entry);
}
}

private IClasspathEntry findMavenStandardSourceFolder() {
return findClasspathEntry(SRC_MAIN_JAVA);
}

private IClasspathEntry findMavenStandardResourceFolder() {
return findClasspathEntry(SRC_MAIN_RESOURCES);
}

private IClasspathEntry findMavenStandardTestSourceFolder() {
return findClasspathEntry(SRC_TEST_JAVA);
}

private IClasspathEntry findMavenStandardTestResourceFolder() {
return findClasspathEntry(SRC_TEST_RESOURCES);
}

private IClasspathEntry findClasspathEntry(String substring) {
for (IClasspathEntry classpathEntry : originalClasspathEntries) {
String path = classpathEntry.getPath().toString();
if (path.contains(substring)) {
return classpathEntry;
}
}
return null;
}

public void configure(MavenAndroidProject mavenProject, EclipseAndroidProject eclipseProject,
IClasspathDescriptor classpath) {
arrayEntries = new ArrayList<IClasspathEntry>();

IJavaProject javaproject = JavaCore.create(eclipseProject.getProject());

originalClasspathEntries = javaproject.readRawClasspath();

addMavenSourceFolder();
addMavenResourceFolder();
addMavenTestSourceFolder();
addMavenTestResourceFolder();
addTheRest();


IClasspathEntry[] newEntries = new IClasspathEntry[arrayEntries.size()];
arrayEntries.toArray(newEntries);

try {
javaproject.setRawClasspath(newEntries, new NullProgressMonitor());
javaproject.save(new NullProgressMonitor(), false);
} catch (Exception ex) {
Log.warn(ex.getMessage());
}

}

}

0 comments on commit 8ed6684

Please sign in to comment.