use of org.apache.tools.ant.Project in project hudson-2.x by hudson.
the class WindowsInstallerLink method doRestart.
public void doRestart(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
if (installationDir == null) {
// if the user reloads the page after Hudson has restarted,
// it comes back here. In such a case, don't let this restart Hudson.
// so just send them back to the top page
rsp.sendRedirect(req.getContextPath() + "/");
return;
}
Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
rsp.forward(this, "_restart", req);
final File oldRoot = Hudson.getInstance().getRootDir();
// initiate an orderly shutdown after we finished serving this request
new Thread("terminator") {
public void run() {
try {
Thread.sleep(1000);
// let the service start after we close our sockets, to avoid conflicts
Runtime.getRuntime().addShutdownHook(new Thread("service starter") {
public void run() {
try {
if (!oldRoot.equals(installationDir)) {
LOGGER.info("Moving data");
Move mv = new Move();
Project p = new Project();
p.addBuildListener(createLogger());
mv.setProject(p);
FileSet fs = new FileSet();
fs.setDir(oldRoot);
// we can't really move the exploded war.
fs.setExcludes("war/**");
mv.addFileset(fs);
mv.setTodir(installationDir);
// plugins can also fail to move
mv.setFailOnError(false);
mv.execute();
}
LOGGER.info("Starting a Windows service");
StreamTaskListener task = StreamTaskListener.fromStdout();
int r = WindowsSlaveInstaller.runElevated(new File(installationDir, "hudson.exe"), "start", task, installationDir);
task.getLogger().println(r == 0 ? "Successfully started" : "start service failed. Exit code=" + r);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private DefaultLogger createLogger() {
DefaultLogger logger = new DefaultLogger();
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.err);
return logger;
}
});
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
use of org.apache.tools.ant.Project in project hudson-2.x by hudson.
the class ClassicPluginStrategy method parseClassPath.
private static void parseClassPath(Manifest manifest, File archive, List<File> paths, String attributeName, String separator) throws IOException {
String classPath = manifest.getMainAttributes().getValue(attributeName);
// attribute not found
if (classPath == null)
return;
for (String s : classPath.split(separator)) {
File file = resolve(archive, s);
if (file.getName().contains("*")) {
// handle wildcard
FileSet fs = new FileSet();
File dir = file.getParentFile();
fs.setDir(dir);
fs.setIncludes(file.getName());
for (String included : fs.getDirectoryScanner(new Project()).getIncludedFiles()) {
paths.add(new File(dir, included));
}
} else {
if (!file.exists())
throw new IOException("No such file: " + file);
paths.add(file);
}
}
}
use of org.apache.tools.ant.Project in project hudson-2.x by hudson.
the class FilePath method validateAntFileMask.
/**
* Validates the ant file mask (like "foo/bar/*.txt, zot/*.jar")
* against this directory, and try to point out the problem.
*
* <p>
* This is useful in conjunction with {@link FormValidation}.
*
* @return
* null if no error was found. Otherwise returns a human readable error message.
* @since 1.90
* @see #validateFileMask(FilePath, String)
*/
public String validateAntFileMask(final String fileMasks) throws IOException, InterruptedException {
return act(new FileCallable<String>() {
public String invoke(File dir, VirtualChannel channel) throws IOException {
if (fileMasks.startsWith("~"))
return Messages.FilePath_TildaDoesntWork();
StringTokenizer tokens = new StringTokenizer(fileMasks, ",");
while (tokens.hasMoreTokens()) {
final String fileMask = tokens.nextToken().trim();
if (hasMatch(dir, fileMask))
// no error on this portion
continue;
// so see if we can match by using ' ' as the separator
if (fileMask.contains(" ")) {
boolean matched = true;
for (String token : Util.tokenize(fileMask)) matched &= hasMatch(dir, token);
if (matched)
return Messages.FilePath_validateAntFileMask_whitespaceSeprator();
}
// a common mistake is to assume the wrong base dir, and there are two variations
// to this: (1) the user gave us aa/bb/cc/dd where cc/dd was correct
// and (2) the user gave us cc/dd where aa/bb/cc/dd was correct.
{
// check the (1) above first
String f = fileMask;
while (true) {
int idx = findSeparator(f);
if (idx == -1)
break;
f = f.substring(idx + 1);
if (hasMatch(dir, f))
return Messages.FilePath_validateAntFileMask_doesntMatchAndSuggest(fileMask, f);
}
}
{
// check the (2) above next as this is more expensive.
// Try prepending "**/" to see if that results in a match
FileSet fs = Util.createFileSet(dir, "**/" + fileMask);
DirectoryScanner ds = fs.getDirectoryScanner(new Project());
if (ds.getIncludedFilesCount() != 0) {
// try shorter name first so that the suggestion results in least amount of changes
String[] names = ds.getIncludedFiles();
Arrays.sort(names, SHORTER_STRING_FIRST);
for (String f : names) {
// now we want to decompose f to the leading portion that matched "**"
// and the trailing portion that matched the file mask, so that
// we can suggest the user error.
//
// this is not a very efficient/clever way to do it, but it's relatively simple
String prefix = "";
while (true) {
int idx = findSeparator(f);
if (idx == -1)
break;
prefix += f.substring(0, idx) + '/';
f = f.substring(idx + 1);
if (hasMatch(dir, prefix + fileMask))
return Messages.FilePath_validateAntFileMask_doesntMatchAndSuggest(fileMask, prefix + fileMask);
}
}
}
}
{
// finally, see if we can identify any sub portion that's valid. Otherwise bail out
String previous = null;
String pattern = fileMask;
while (true) {
if (hasMatch(dir, pattern)) {
// found a match
if (previous == null)
return Messages.FilePath_validateAntFileMask_portionMatchAndSuggest(fileMask, pattern);
else
return Messages.FilePath_validateAntFileMask_portionMatchButPreviousNotMatchAndSuggest(fileMask, pattern, previous);
}
int idx = findSeparator(pattern);
if (idx < 0) {
// no more path component left to go back
if (pattern.equals(fileMask))
return Messages.FilePath_validateAntFileMask_doesntMatchAnything(fileMask);
else
return Messages.FilePath_validateAntFileMask_doesntMatchAnythingAndSuggest(fileMask, pattern);
}
// cut off the trailing component and try again
previous = pattern;
pattern = pattern.substring(0, idx);
}
}
}
// no error
return null;
}
private boolean hasMatch(File dir, String pattern) {
FileSet fs = Util.createFileSet(dir, pattern);
DirectoryScanner ds = fs.getDirectoryScanner(new Project());
return ds.getIncludedFilesCount() != 0 || ds.getIncludedDirsCount() != 0;
}
/**
* Finds the position of the first path separator.
*/
private int findSeparator(String pattern) {
int idx1 = pattern.indexOf('\\');
int idx2 = pattern.indexOf('/');
if (idx1 == -1)
return idx2;
if (idx2 == -1)
return idx1;
return Math.min(idx1, idx2);
}
});
}
use of org.apache.tools.ant.Project in project jsonschema2pojo by joelittlejohn.
the class Jsonschema2PojoTaskIT method invokeAntBuild.
private void invokeAntBuild(String pathToBuildFile) throws URISyntaxException {
File buildFile = new File(this.getClass().getResource(pathToBuildFile).toURI());
Project project = new Project();
project.setUserProperty("ant.file", buildFile.getAbsolutePath());
project.setUserProperty("targetDirectory", schemaRule.getGenerateDir().getAbsolutePath());
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projecthelper", helper);
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
}
use of org.apache.tools.ant.Project in project JessMA by ldcsaa.
the class Tarrer method getTask.
/** 获取压缩任务对象 */
@Override
protected Task getTask() {
Project project = new Project();
Tar tar = new Tar();
tar.setProject(project);
createSourceFileSet(tar);
tar.setDestFile(getTargetFile());
tar.setCompression(getCompressionMethod());
return tar;
}
Aggregations