我寫了一個Ant任務生成一個Eclipse .userlibraries文件。您可以導入生成的文件以在Eclipse中創建用戶庫。然後將此用戶庫用作構建路徑的一部分。
使用任務添加到您的Ant構建文件:
<target name="createEclipseUserLibraries"
description="Creates classpath and bootclasspatch that can be imported into Eclipse">
<taskdef name="createEclipseUserLibraries"
classname="com.forumsys.tools.CreateEclipseUserLibraries"
classpathref="yourclasspathref"/>
<createEclipseUserLibraries classpathref="classpathref" bootclasspathref="bootclasspathref"/>
</target>
Ant任務。它需要ant.jar來運行和編譯:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
/**
* A custom tag to create a file the eclipse can import to setup a user libraries.
*
* Created: Mar 29, 2014 9:44:09 AM
*
* @author <a href="mailto:[email protected]">Javier S. López</a>
* @version 1.0
*/
public class CreateEclipseUserLibraries extends Task {
public static final String UTF8_ENCODING = "UTF-8";
public static final String DEFAULT_BOOT_CLASSPATH_LIBRARY_NAME = "SYSTEM_LIBRARY";
public static final String DEFAULT_CLASSPATH_LIBRARY_NAME = "LIBRARY";
public static final String DEFAULT_DESTINATION = "Eclipse.userlibraries";
private static final String INDENT = " ";
private Path _classpath;
private Path _bootClasspath;
private String _bootClasspathLibraryName = DEFAULT_BOOT_CLASSPATH_LIBRARY_NAME;
private String _classpathLibraryName = DEFAULT_CLASSPATH_LIBRARY_NAME;
private String _destination = DEFAULT_DESTINATION;
public void setClasspath(final Path classpath) {
if (_classpath == null) {
_classpath = classpath;
} else {
_classpath.append(classpath);
}
}
public void setClasspathRef(final Reference reference) {
if (_classpath == null) {
final Project antProject = getProject();
_classpath = new Path(antProject);
}
_classpath.setRefid(reference);
}
public void setBootClasspath(final Path bootClasspath) {
if (_bootClasspath == null) {
_bootClasspath = bootClasspath;
} else {
_bootClasspath.append(bootClasspath);
}
}
public void setBootClasspathRef(final Reference reference) {
if (_bootClasspath == null) {
final Project antProject = getProject();
_bootClasspath = new Path(antProject);
}
_bootClasspath.setRefid(reference);
}
public void setClasspathLibraryName(final String name) {
if (!isEmpty(name)) {
_classpathLibraryName = name;
}
}
public void setBootClasspathLibraryName(final String name) {
if (!isEmpty(name)) {
_bootClasspathLibraryName = name;
}
}
public void setDestination(final String argDestination) {
if (!isEmpty(argDestination)) {
_destination = argDestination;
}
}
@Override
public void execute() throws BuildException {
if (_classpath == null) {
throw new BuildException("classpath or classpathref attribute must be set");
}
if (_bootClasspath == null) {
throw new BuildException("bootclasspath or bootclasspathref attribute must be set");
}
try {
createUserLibrariesFile();
} catch (final IOException e) {
throw new BuildException(e.getMessage(), e);
}
}
/**
* @throws IOException
*
*/
private void createUserLibrariesFile() throws IOException {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<?final xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
stringBuilder.append("\n");
stringBuilder.append("<eclipse-userlibraries version=\"2\">").append("\n");
createBootClasspathLibrary(stringBuilder);
createClasspathLibrary(stringBuilder);
stringBuilder.append("</eclipse-userlibraries>");
final Project antProject = getProject();
final File baseDir = antProject.getBaseDir();
final File file = new File(baseDir, _destination);
if (file.exists()) {
file.delete();
}
final boolean append = false;
BufferedOutputStream bos = null;
try {
final FileOutputStream fos = new FileOutputStream(file, append);
bos = new BufferedOutputStream(fos);
bos.write(stringBuilder.toString().getBytes(UTF8_ENCODING));
bos.flush();
} finally {
if (bos != null) {
bos.close();
}
}
}
/**
* @param stringBuilder
*
*/
private void createBootClasspathLibrary(final StringBuilder stringBuilder) {
createLibrary(stringBuilder, _bootClasspathLibraryName, true, _bootClasspath);
}
/**
* @param stringBuilder
*/
private void createClasspathLibrary(final StringBuilder stringBuilder) {
createLibrary(stringBuilder, _classpathLibraryName, false, _classpath);
}
/**
* @param stringBuilder
* @param bootClasspathLibraryName
* @param b
* @param bootClasspath
*/
private void createLibrary(final StringBuilder stringBuilder, final String libraryName,
final boolean isSystemLibrary, final Path path) {
stringBuilder.append(INDENT).append("<library name=\"").append(libraryName);
stringBuilder.append("\" systemlibrary=\"").append(Boolean.toString(isSystemLibrary)).append("\">\n");
final String[] paths = path.list();
final Project antProject = getProject();
final File baseDir = antProject.getBaseDir();
final String baseDirName = baseDir.getName();
for (final String strPath : paths) {
final int index = strPath.indexOf(baseDirName);
//Only include the relative path
if (index != -1) {
stringBuilder.append(INDENT).append(INDENT);
stringBuilder.append("<archive path=\"").append(
strPath.substring(index - 1)).append("\"/>\n");
}
}
stringBuilder.append(INDENT).append("</library>\n");
}
public static final boolean isEmpty(final String str) {
return (str == null) || (str.length() == 0);
}
}
謝謝,這很有幫助。我很希望有人已經開發出你描述的那種腳本。特別是,這些構建文件有很多條件包含,我想自動解決它們。 – 2010-03-16 15:09:58