aQute.bnd.ant.BndTask Maven / Gradle / Ivy
The newest version!
package aQute.bnd.ant;
import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Property;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import aQute.bnd.build.Project;
import aQute.bnd.build.Workspace;
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.eclipse.EclipseClasspath;
import aQute.lib.utf8properties.UTF8Properties;
import aQute.libg.qtokens.QuotedTokenizer;
/**
*
* This file is the bnd main task for ant.
*
*
* To define the task library, load property from build.bnd
and
* prepare the workspace:
*
*
*
* <target name="init" unless="initialized"> <taskdef
* classpath="${path.to.bnd.jar}"
* resource="aQute/bnd/ant/taskdef.properties"> <bndprepare
* basedir="${projectdir}" print="false" top="${release.dir}"/> <property
* name="initialized" value="set"/> </target>
*
*
* To recursively build dependency projects, before building this project:
*
*
*
* <target name="dependencies" depends="init" if="project.dependson"
* unless="donotrecurse"> <subant target="build" inheritAll="false"
* buildpath="${project.dependson}"> <property name="donotrecurse"
* value="true"/> </subant> </target>
*
*
* To build a bundle:
*
*
*
* <target name="build" depends="compile"> <mkdir
* dir="${target}"/> <bnd command="build" exceptions="true"
* basedir="${project}"/> </target>
*
*
* To pass properties into bnd from ANT:
*
*
*
* <target name="build" depends="compile">
* <mkdir dir="${target}"/> <bnd command="build" exceptions="true"
* basedir="${project}"> <!-- Property will be set on the bnd Project:
* --> <property name="foo" value="bar"/> <!-- Property will be set
* on the bnd Workspace: --> <wsproperty name="foo" value="bar"/>
* </bnd> </target>
*
*
* @see DeployTask
* @see ReleaseTask
*/
/*
* OLD JAVADOCS:
*
*
*
*
*
*
*/
public class BndTask extends BaseTask {
private final static Logger logger = LoggerFactory.getLogger(BndTask.class);
String command;
File basedir;
boolean test;
boolean failok;
boolean exceptions;
boolean print;
// flags aiming to know how classpath & bnd descriptors were set
private boolean classpathDirectlySet;
private Path classpathReference;
private Path bndfilePath;
@Override
public void execute() throws BuildException {
// JME add - ensure every required parameter is present
// handle cases where mutual exclusion live..
// this is the ANT tradition ..
validate();
updateClasspath();
updateBndFiles();
if (command == null) {
executeBackwardCompatible();
return;
}
if (basedir == null)
throw new BuildException("No basedir set");
try {
Project project = Workspace.getProject(basedir);
Workspace ws = project.getWorkspace();
for (Property prop : workspaceProps) {
ws.setProperty(prop.getName(), prop.getValue());
}
project.setProperty("in.ant", "true");
project.setProperty("environment", "ant");
project.setExceptions(true);
project.setTrace(trace);
project.setPedantic(pedantic);
for (Property prop : properties) {
project.setProperty(prop.getName(), prop.getValue());
}
if (test)
project.action(command, test);
else
project.action(command);
for (Project p : ws.getCurrentProjects())
ws.getInfo(p, p + ":");
if (report(ws))
throw new BuildException("Command " + command + " failed");
} catch (Throwable e) {
if (exceptions)
e.printStackTrace();
throw new BuildException(e);
}
}
public void setCommand(String command) {
this.command = command;
}
/**
* Set the base directory of the project. This property MUST be set.
*
* @param basedir
*/
public void setBasedir(File basedir) {
this.basedir = basedir;
}
// Old shit
List files = new ArrayList<>();
List classpath = new ArrayList<>();
List sourcepath = new ArrayList<>();
File output = null;
File testDir = null;
boolean eclipse;
boolean inherit = true;
@SuppressWarnings("cast")
private void executeBackwardCompatible() throws BuildException {
try {
if (files == null)
throw new BuildException("No files set");
if (eclipse) {
File project = getProject().getBaseDir();
EclipseClasspath cp = new EclipseClasspath(this, project.getParentFile(), project);
classpath.addAll(cp.getClasspath());
classpath.addAll(cp.getBootclasspath());
sourcepath.addAll(cp.getSourcepath());
// classpath.add(cp.getOutput());
if (report())
throw new BuildException("Errors during Eclipse Path inspection");
}
if (output == null)
output = getProject().getBaseDir();
for (Iterator f = files.iterator(); f.hasNext();) {
File file = f.next();
Builder builder = new Builder();
builder.setPedantic(isPedantic());
if (file.exists()) {
// Do nice property calculations
// merging includes etc.
builder.setProperties(file);
}
// get them and merge them with the project
// properties, if the inherit flag is specified
if (inherit) {
Properties projectProperties = new UTF8Properties();
@SuppressWarnings("unchecked")
Hashtable