mirror of https://github.com/apache/ant-ivy
358 lines
12 KiB
Java
358 lines
12 KiB
Java
/*
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
* this work for additional information regarding copyright ownership.
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
* (the "License"); you may not use this file except in compliance with
|
|
* the License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
*/
|
|
package org.apache.ivy.ant;
|
|
|
|
import java.io.File;
|
|
import java.net.MalformedURLException;
|
|
import java.text.ParseException;
|
|
|
|
import org.apache.ivy.Ivy;
|
|
import org.apache.ivy.core.cache.CacheManager;
|
|
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
|
|
import org.apache.ivy.core.module.id.ModuleRevisionId;
|
|
import org.apache.ivy.core.report.ResolveReport;
|
|
import org.apache.ivy.core.resolve.ResolveOptions;
|
|
import org.apache.ivy.core.settings.IvySettings;
|
|
import org.apache.ivy.util.Message;
|
|
import org.apache.ivy.util.filter.FilterHelper;
|
|
import org.apache.tools.ant.BuildException;
|
|
import org.apache.tools.ant.Project;
|
|
|
|
/**
|
|
* This task allow to call the Ivy dependency resolution from ant.
|
|
*/
|
|
public class IvyResolve extends IvyTask {
|
|
private File file = null;
|
|
|
|
private String conf = null;
|
|
|
|
private File cache = null;
|
|
|
|
private String organisation = null;
|
|
|
|
private String module = null;
|
|
|
|
private String revision = null;
|
|
|
|
private String pubdate = null;
|
|
|
|
private boolean inline = false;
|
|
|
|
private boolean haltOnFailure = true;
|
|
|
|
private boolean useCacheOnly = false;
|
|
|
|
private String type = null;
|
|
|
|
private boolean transitive = true;
|
|
|
|
private boolean changing = false;
|
|
|
|
private Boolean keep = null;
|
|
|
|
private String failureProperty = null;
|
|
|
|
private boolean useOrigin = false;
|
|
|
|
private String resolveId = null;
|
|
|
|
public boolean isUseOrigin() {
|
|
return useOrigin;
|
|
}
|
|
|
|
public void setUseOrigin(boolean useOrigin) {
|
|
this.useOrigin = useOrigin;
|
|
}
|
|
|
|
public String getDate() {
|
|
return pubdate;
|
|
}
|
|
|
|
public void setDate(String pubdate) {
|
|
this.pubdate = pubdate;
|
|
}
|
|
|
|
public String getRevision() {
|
|
return revision;
|
|
}
|
|
|
|
public void setRevision(String revision) {
|
|
this.revision = revision;
|
|
}
|
|
|
|
public File getCache() {
|
|
return cache;
|
|
}
|
|
|
|
public void setCache(File cache) {
|
|
this.cache = cache;
|
|
}
|
|
|
|
public String getConf() {
|
|
return conf;
|
|
}
|
|
|
|
public void setConf(String conf) {
|
|
this.conf = conf;
|
|
}
|
|
|
|
public File getFile() {
|
|
return file;
|
|
}
|
|
|
|
public void setFile(File file) {
|
|
this.file = file;
|
|
}
|
|
|
|
public boolean isHaltonfailure() {
|
|
return haltOnFailure;
|
|
}
|
|
|
|
public void setHaltonfailure(boolean haltOnFailure) {
|
|
this.haltOnFailure = haltOnFailure;
|
|
}
|
|
|
|
public void setShowprogress(boolean show) {
|
|
Message.setShowProgress(show);
|
|
}
|
|
|
|
public boolean isUseCacheOnly() {
|
|
return useCacheOnly;
|
|
}
|
|
|
|
public void setUseCacheOnly(boolean useCacheOnly) {
|
|
this.useCacheOnly = useCacheOnly;
|
|
}
|
|
|
|
public String getType() {
|
|
return type;
|
|
}
|
|
|
|
public void setType(String type) {
|
|
this.type = type;
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use {@link #setFailureProperty(String)} instead
|
|
*/
|
|
public void setFailurePropery(String failureProperty) {
|
|
log("The 'failurepropery' attribute is deprecated. "
|
|
+ "Please use the 'failureproperty' attribute instead", Project.MSG_WARN);
|
|
setFailureProperty(failureProperty);
|
|
}
|
|
|
|
public void setFailureProperty(String failureProperty) {
|
|
this.failureProperty = failureProperty;
|
|
}
|
|
|
|
public String getFailureProperty() {
|
|
return failureProperty;
|
|
}
|
|
|
|
public void doExecute() throws BuildException {
|
|
Ivy ivy = getIvyInstance();
|
|
IvySettings settings = ivy.getSettings();
|
|
try {
|
|
conf = getProperty(conf, settings, "ivy.configurations");
|
|
type = getProperty(type, settings, "ivy.resolve.default.type.filter");
|
|
if (cache == null) {
|
|
cache = settings.getDefaultCache();
|
|
}
|
|
String[] confs = splitConfs(conf);
|
|
|
|
ResolveReport report;
|
|
if (isInline()) {
|
|
if (organisation == null) {
|
|
throw new BuildException("'organisation' is required when using inline mode");
|
|
}
|
|
if (module == null) {
|
|
throw new BuildException("'module' is required when using inline mode");
|
|
}
|
|
if (file != null) {
|
|
throw new BuildException("'file' not allowed when using inline mode");
|
|
}
|
|
if (revision == null) {
|
|
revision = "latest.integration";
|
|
}
|
|
report = ivy.resolve(ModuleRevisionId
|
|
.newInstance(organisation, module, revision), getResolveOptions(confs,
|
|
settings), changing);
|
|
|
|
} else {
|
|
if (organisation != null) {
|
|
throw new BuildException(
|
|
"'organisation' not allowed when not using 'org' attribute");
|
|
}
|
|
if (module != null) {
|
|
throw new BuildException("'module' not allowed when not using 'org' attribute");
|
|
}
|
|
if (file == null) {
|
|
file = getProject().resolveFile(getProperty(settings, "ivy.dep.file"));
|
|
}
|
|
report = ivy.resolve(file.toURL(), getResolveOptions(confs, settings));
|
|
}
|
|
if (report.hasError()) {
|
|
if (failureProperty != null) {
|
|
getProject().setProperty(failureProperty, "true");
|
|
}
|
|
if (isHaltonfailure()) {
|
|
throw new BuildException("resolve failed - see output for details");
|
|
}
|
|
}
|
|
setResolved(report, resolveId, isKeep());
|
|
|
|
if (isKeep()) {
|
|
ModuleDescriptor md = report.getModuleDescriptor();
|
|
// put resolved infos in ant properties and ivy variables
|
|
// putting them in ivy variables is important to be able to change from one resolve
|
|
// call to the other
|
|
getProject().setProperty("ivy.organisation",
|
|
md.getModuleRevisionId().getOrganisation());
|
|
settings.setVariable(
|
|
"ivy.organisation", md.getModuleRevisionId().getOrganisation());
|
|
getProject().setProperty("ivy.module", md.getModuleRevisionId().getName());
|
|
settings.setVariable("ivy.module", md.getModuleRevisionId().getName());
|
|
getProject().setProperty("ivy.revision",
|
|
md.getResolvedModuleRevisionId().getRevision());
|
|
settings.setVariable(
|
|
"ivy.revision", md.getResolvedModuleRevisionId().getRevision());
|
|
boolean hasChanged = report.hasChanged();
|
|
getProject().setProperty("ivy.deps.changed", String.valueOf(hasChanged));
|
|
settings.setVariable("ivy.deps.changed", String.valueOf(hasChanged));
|
|
if (conf.trim().equals("*")) {
|
|
getProject().setProperty("ivy.resolved.configurations",
|
|
mergeConfs(md.getConfigurationsNames()));
|
|
settings.setVariable("ivy.resolved.configurations", mergeConfs(md
|
|
.getConfigurationsNames()));
|
|
} else {
|
|
getProject().setProperty("ivy.resolved.configurations", conf);
|
|
settings.setVariable("ivy.resolved.configurations", conf);
|
|
}
|
|
if (file != null) {
|
|
getProject().setProperty("ivy.resolved.file", file.getAbsolutePath());
|
|
settings.setVariable("ivy.resolved.file", file.getAbsolutePath());
|
|
}
|
|
if (resolveId != null) {
|
|
getProject().setProperty("ivy.organisation." + resolveId,
|
|
md.getModuleRevisionId().getOrganisation());
|
|
settings.setVariable("ivy.organisation." + resolveId, md.getModuleRevisionId()
|
|
.getOrganisation());
|
|
getProject().setProperty("ivy.module." + resolveId,
|
|
md.getModuleRevisionId().getName());
|
|
settings.setVariable("ivy.module." + resolveId, md.getModuleRevisionId()
|
|
.getName());
|
|
getProject().setProperty("ivy.revision." + resolveId,
|
|
md.getResolvedModuleRevisionId().getRevision());
|
|
settings.setVariable("ivy.revision." + resolveId, md
|
|
.getResolvedModuleRevisionId().getRevision());
|
|
getProject().setProperty("ivy.deps.changed." + resolveId,
|
|
String.valueOf(hasChanged));
|
|
settings.setVariable("ivy.deps.changed." + resolveId, String
|
|
.valueOf(hasChanged));
|
|
if (conf.trim().equals("*")) {
|
|
getProject().setProperty("ivy.resolved.configurations." + resolveId,
|
|
mergeConfs(md.getConfigurationsNames()));
|
|
settings.setVariable("ivy.resolved.configurations." + resolveId,
|
|
mergeConfs(md.getConfigurationsNames()));
|
|
} else {
|
|
getProject()
|
|
.setProperty("ivy.resolved.configurations." + resolveId, conf);
|
|
settings.setVariable("ivy.resolved.configurations." + resolveId, conf);
|
|
}
|
|
getProject().setProperty("ivy.resolved.file." + resolveId,
|
|
file.getAbsolutePath());
|
|
settings
|
|
.setVariable("ivy.resolved.file." + resolveId, file.getAbsolutePath());
|
|
}
|
|
}
|
|
} catch (MalformedURLException e) {
|
|
throw new BuildException(
|
|
"unable to convert given ivy file to url: " + file + ": " + e, e);
|
|
} catch (ParseException e) {
|
|
log(e.getMessage(), Project.MSG_ERR);
|
|
throw new BuildException("syntax errors in ivy file: " + e, e);
|
|
} catch (Exception e) {
|
|
throw new BuildException("impossible to resolve dependencies: " + e, e);
|
|
}
|
|
}
|
|
|
|
private ResolveOptions getResolveOptions(String[] confs, IvySettings settings) {
|
|
return new ResolveOptions().setConfs(confs).setValidate(doValidate(settings))
|
|
.setArtifactFilter(FilterHelper.getArtifactTypeFilter(type))
|
|
.setRevision(revision).setCache(CacheManager.getInstance(settings, cache))
|
|
.setDate(getPubDate(pubdate, null)).setUseCacheOnly(useCacheOnly).setUseOrigin(
|
|
useOrigin).setTransitive(transitive).setResolveId(resolveId);
|
|
}
|
|
|
|
public String getModule() {
|
|
return module;
|
|
}
|
|
|
|
public void setModule(String module) {
|
|
this.module = module;
|
|
}
|
|
|
|
public String getOrganisation() {
|
|
return organisation;
|
|
}
|
|
|
|
public void setOrganisation(String organisation) {
|
|
this.organisation = organisation;
|
|
}
|
|
|
|
public boolean isTransitive() {
|
|
return transitive;
|
|
}
|
|
|
|
public void setTransitive(boolean transitive) {
|
|
this.transitive = transitive;
|
|
}
|
|
|
|
public boolean isChanging() {
|
|
return changing;
|
|
}
|
|
|
|
public void setChanging(boolean changing) {
|
|
this.changing = changing;
|
|
}
|
|
|
|
public boolean isKeep() {
|
|
return keep == null ? organisation == null : keep.booleanValue();
|
|
}
|
|
|
|
public void setKeep(boolean keep) {
|
|
this.keep = Boolean.valueOf(keep);
|
|
}
|
|
|
|
public boolean isInline() {
|
|
return inline;
|
|
}
|
|
|
|
public void setInline(boolean inline) {
|
|
this.inline = inline;
|
|
}
|
|
|
|
public String getResolveId() {
|
|
return resolveId;
|
|
}
|
|
|
|
public void setResolveId(String resolveId) {
|
|
this.resolveId = resolveId;
|
|
}
|
|
}
|