2
0
Fork 0
mirror of https://github.com/ethauvin/bld.git synced 2025-04-26 00:37:10 -07:00

Better error reporting when internet is not accessible, added support for offline mode that will not access the internet for any reason.

This commit is contained in:
Geert Bevin 2024-07-17 20:59:38 -04:00
parent 8e02a3ac7e
commit d904fd22b7
6 changed files with 112 additions and 59 deletions

View file

@ -485,7 +485,12 @@ public class BaseProject extends BuildExecutor {
@BuildCommand(value = "dependency-tree", help = DependencyTreeHelp.class)
public void dependencyTree()
throws Exception {
dependencyTreeOperation().executeOnce(() -> dependencyTreeOperation().fromProject(this));
if (isOffline()) {
System.out.println("Offline mode: dependency-tree is disabled");
}
else {
dependencyTreeOperation().executeOnce(() -> dependencyTreeOperation().fromProject(this));
}
}
/**
@ -496,7 +501,12 @@ public class BaseProject extends BuildExecutor {
@BuildCommand(help = DownloadHelp.class)
public void download()
throws Exception {
downloadOperation().executeOnce(() -> downloadOperation().fromProject(this));
if (isOffline()) {
System.out.println("Offline mode: download is disabled");
}
else {
downloadOperation().executeOnce(() -> downloadOperation().fromProject(this));
}
}
/**
@ -507,7 +517,12 @@ public class BaseProject extends BuildExecutor {
@BuildCommand(help = PurgeHelp.class)
public void purge()
throws Exception {
purgeOperation().executeOnce(() -> purgeOperation().fromProject(this));
if (isOffline()) {
System.out.println("Offline mode: purge is disabled");
}
else {
purgeOperation().executeOnce(() -> purgeOperation().fromProject(this));
}
}
/**
@ -1674,7 +1689,8 @@ public class BaseProject extends BuildExecutor {
@Override
public int execute(String[] arguments) {
if (autoDownloadPurge()) {
if (!isOffline() &&
autoDownloadPurge()) {
performAutoDownloadPurge();
}

View file

@ -34,6 +34,7 @@ public class BuildExecutor {
public static final String BLD_PROPERTIES = "bld.properties";
public static final String LOCAL_PROPERTIES = "local.properties";
private static final String ARG_OFFLINE = "--offline";
private static final String ARG_HELP1 = "--help";
private static final String ARG_HELP2 = "-h";
private static final String ARG_HELP3 = "-?";
@ -42,6 +43,7 @@ public class BuildExecutor {
private final HierarchicalProperties properties_;
private List<String> arguments_ = Collections.emptyList();
private boolean offline_ = false;
private Map<String, CommandDefinition> buildCommands_ = null;
private Map<String, String> buildAliases_ = null;
private final AtomicReference<String> currentCommandName_ = new AtomicReference<>();
@ -126,7 +128,18 @@ public class BuildExecutor {
}
/**
* Returns the properties uses by this conversation.
* Returns whether the bld execution is intended to be offline.
*
* @return {@code true} if the execution is intended to be offline;
* or {@code false} otherwise
* @since 2.0
*/
public boolean isOffline() {
return offline_;
}
/**
* Returns the properties uses for bld execution.
*
* @return the instance of {@code HierarchicalProperties} that is used
* by this build executor
@ -275,6 +288,11 @@ public class BuildExecutor {
* @since 1.5.1
*/
public void start(String[] arguments) {
if (arguments.length > 0 && arguments[0].equals(ARG_OFFLINE)) {
offline_ = true;
arguments = Arrays.copyOfRange(arguments, 1, arguments.length);
}
System.exit(execute(arguments));
}

View file

@ -191,7 +191,12 @@ public class Project extends BaseProject {
jar();
jarSources();
jarJavadoc();
publishOperation().executeOnce(() -> publishOperation().fromProject(this));
if (isOffline()) {
System.out.println("Offline mode: publish is disabled");
}
else {
publishOperation().executeOnce(() -> publishOperation().fromProject(this));
}
}
/**

View file

@ -155,6 +155,7 @@ public class HelpOperation {
System.err.println("""
--offline Work without internet (only as first argument)
--json Output help in JSON format
-?, -h, --help Shows this help message
-D<name>=<value> Set a JVM system property

View file

@ -41,6 +41,7 @@ public class Wrapper {
public static final String BUILD_ARGUMENT = "--build";
public static final String JSON_ARGUMENT = "--json";
public static final String OFFLINE_ARGUMENT = "--offline";
public static final String HELP_COMMAND = "help";
static final String MAVEN_CENTRAL = "https://repo1.maven.org/maven2/";
@ -79,6 +80,7 @@ public class Wrapper {
private File currentDir_ = new File(System.getProperty("user.dir"));
private LaunchMode launchMode_ = LaunchMode.Cli;
private boolean offline_ = false;
private final Properties jvmProperties_ = new Properties();
private final Properties wrapperProperties_ = new Properties();
@ -389,6 +391,15 @@ public class Wrapper {
arguments.remove(0);
}
// first argument after the --build argument is the main build file
// arguments.get(0)
// check if the next argument enables offline mode
if (arguments.size() >= 2 &&
OFFLINE_ARGUMENT.equals(arguments.get(1))) {
offline_ = true;
}
var help_index = arguments.indexOf(HELP_COMMAND);
if (help_index != -1) {
try {
@ -405,12 +416,7 @@ public class Wrapper {
try {
extractJvmProperties(arguments);
initWrapperProperties(getVersion());
File distribution;
try {
distribution = installDistribution();
} catch (IOException e) {
return -1;
}
var distribution = installDistribution();
return launchMain(distribution, arguments);
} catch (Exception e) {
throw new RuntimeException(e);
@ -513,25 +519,29 @@ public class Wrapper {
var download_version = version;
var is_snapshot = isSnapshot(version);
var is_local = false;
if (is_snapshot) {
var meta_data = "";
try {
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata.xml")));
}
catch (IOException e) {
if (offline_) {
System.out.println("Offline mode: no artifacts will be checked nor downloaded");
System.out.flush();
}
else {
if (is_snapshot) {
var meta_data = "";
try {
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata-local.xml")));
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata.xml")));
} catch (IOException e) {
try {
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata-local.xml")));
} catch (IOException e2) {
throw e;
}
}
catch (IOException e2) {
throw e;
}
}
var local_matcher = META_DATA_LOCAL_COPY.matcher(meta_data);
is_local = local_matcher.find();
if (!is_local) {
var version_matcher = META_DATA_SNAPSHOT_VERSION.matcher(meta_data);
if (version_matcher.find()) {
download_version = version_matcher.group(1);
var local_matcher = META_DATA_LOCAL_COPY.matcher(meta_data);
is_local = local_matcher.find();
if (!is_local) {
var version_matcher = META_DATA_SNAPSHOT_VERSION.matcher(meta_data);
if (version_matcher.find()) {
download_version = version_matcher.group(1);
}
}
}
}
@ -539,40 +549,42 @@ public class Wrapper {
var distribution_file = new File(DISTRIBUTIONS_DIR, bldFileName(version));
var distribution_sources_file = new File(DISTRIBUTIONS_DIR, bldSourcesFileName(version));
// if this is a snapshot and the distribution file exists,
// ensure that it's the latest by comparing hashes
if (is_snapshot && distribution_file.exists()) {
boolean delete_distribution_files = is_local;
if (!delete_distribution_files) {
var download_md5 = readString(version, new URL(downloadUrl(version, bldFileName(download_version)) + ".md5"));
try {
var digest = MessageDigest.getInstance("MD5");
digest.update(FileUtils.readBytes(distribution_file));
if (!download_md5.equals(encodeHexLower(digest.digest()))) {
delete_distribution_files = true;
if (!offline_) {
// if this is a snapshot and the distribution file exists,
// ensure that it's the latest by comparing hashes
if (is_snapshot && distribution_file.exists()) {
boolean delete_distribution_files = is_local;
if (!delete_distribution_files) {
var download_md5 = readString(version, new URL(downloadUrl(version, bldFileName(download_version)) + ".md5"));
try {
var digest = MessageDigest.getInstance("MD5");
digest.update(FileUtils.readBytes(distribution_file));
if (!download_md5.equals(encodeHexLower(digest.digest()))) {
delete_distribution_files = true;
}
} catch (NoSuchAlgorithmException ignore) {
}
} catch (NoSuchAlgorithmException ignore) {
}
if (delete_distribution_files) {
distribution_file.delete();
if (distribution_sources_file.exists()) {
distribution_sources_file.delete();
}
}
}
if (delete_distribution_files) {
distribution_file.delete();
if (distribution_sources_file.exists()) {
distribution_sources_file.delete();
}
// download distribution jars if necessary
if (!distribution_file.exists()) {
downloadDistribution(distribution_file, downloadUrl(version, bldFileName(download_version)));
}
}
// download distribution jars if necessary
if (!distribution_file.exists()) {
downloadDistribution(distribution_file, downloadUrl(version, bldFileName(download_version)));
}
if (!distribution_sources_file.exists()) {
try {
downloadDistribution(distribution_sources_file, downloadUrl(version, bldSourcesFileName(download_version)));
} catch (IOException e) {
// this is not critical, ignore
if (!distribution_sources_file.exists()) {
try {
downloadDistribution(distribution_sources_file, downloadUrl(version, bldSourcesFileName(download_version)));
} catch (IOException e) {
// this is not critical, ignore
}
}
}
@ -618,7 +630,8 @@ public class Wrapper {
private void resolveExtensions() {
if (null == classloader_ ||
null == wrapperPropertiesFile_) {
null == wrapperPropertiesFile_ ||
offline_) {
return;
}