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

Binary file not shown.

View file

@ -485,7 +485,12 @@ public class BaseProject extends BuildExecutor {
@BuildCommand(value = "dependency-tree", help = DependencyTreeHelp.class) @BuildCommand(value = "dependency-tree", help = DependencyTreeHelp.class)
public void dependencyTree() public void dependencyTree()
throws Exception { 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) @BuildCommand(help = DownloadHelp.class)
public void download() public void download()
throws Exception { 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) @BuildCommand(help = PurgeHelp.class)
public void purge() public void purge()
throws Exception { 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 @Override
public int execute(String[] arguments) { public int execute(String[] arguments) {
if (autoDownloadPurge()) { if (!isOffline() &&
autoDownloadPurge()) {
performAutoDownloadPurge(); performAutoDownloadPurge();
} }

View file

@ -34,6 +34,7 @@ public class BuildExecutor {
public static final String BLD_PROPERTIES = "bld.properties"; public static final String BLD_PROPERTIES = "bld.properties";
public static final String LOCAL_PROPERTIES = "local.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_HELP1 = "--help";
private static final String ARG_HELP2 = "-h"; private static final String ARG_HELP2 = "-h";
private static final String ARG_HELP3 = "-?"; private static final String ARG_HELP3 = "-?";
@ -42,6 +43,7 @@ public class BuildExecutor {
private final HierarchicalProperties properties_; private final HierarchicalProperties properties_;
private List<String> arguments_ = Collections.emptyList(); private List<String> arguments_ = Collections.emptyList();
private boolean offline_ = false;
private Map<String, CommandDefinition> buildCommands_ = null; private Map<String, CommandDefinition> buildCommands_ = null;
private Map<String, String> buildAliases_ = null; private Map<String, String> buildAliases_ = null;
private final AtomicReference<String> currentCommandName_ = new AtomicReference<>(); 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 * @return the instance of {@code HierarchicalProperties} that is used
* by this build executor * by this build executor
@ -275,6 +288,11 @@ public class BuildExecutor {
* @since 1.5.1 * @since 1.5.1
*/ */
public void start(String[] arguments) { 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)); System.exit(execute(arguments));
} }

View file

@ -191,7 +191,12 @@ public class Project extends BaseProject {
jar(); jar();
jarSources(); jarSources();
jarJavadoc(); 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(""" System.err.println("""
--offline Work without internet (only as first argument)
--json Output help in JSON format --json Output help in JSON format
-?, -h, --help Shows this help message -?, -h, --help Shows this help message
-D<name>=<value> Set a JVM system property -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 BUILD_ARGUMENT = "--build";
public static final String JSON_ARGUMENT = "--json"; public static final String JSON_ARGUMENT = "--json";
public static final String OFFLINE_ARGUMENT = "--offline";
public static final String HELP_COMMAND = "help"; public static final String HELP_COMMAND = "help";
static final String MAVEN_CENTRAL = "https://repo1.maven.org/maven2/"; 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 File currentDir_ = new File(System.getProperty("user.dir"));
private LaunchMode launchMode_ = LaunchMode.Cli; private LaunchMode launchMode_ = LaunchMode.Cli;
private boolean offline_ = false;
private final Properties jvmProperties_ = new Properties(); private final Properties jvmProperties_ = new Properties();
private final Properties wrapperProperties_ = new Properties(); private final Properties wrapperProperties_ = new Properties();
@ -389,6 +391,15 @@ public class Wrapper {
arguments.remove(0); 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); var help_index = arguments.indexOf(HELP_COMMAND);
if (help_index != -1) { if (help_index != -1) {
try { try {
@ -405,12 +416,7 @@ public class Wrapper {
try { try {
extractJvmProperties(arguments); extractJvmProperties(arguments);
initWrapperProperties(getVersion()); initWrapperProperties(getVersion());
File distribution; var distribution = installDistribution();
try {
distribution = installDistribution();
} catch (IOException e) {
return -1;
}
return launchMain(distribution, arguments); return launchMain(distribution, arguments);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -513,25 +519,29 @@ public class Wrapper {
var download_version = version; var download_version = version;
var is_snapshot = isSnapshot(version); var is_snapshot = isSnapshot(version);
var is_local = false; var is_local = false;
if (is_snapshot) { if (offline_) {
var meta_data = ""; System.out.println("Offline mode: no artifacts will be checked nor downloaded");
try { System.out.flush();
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata.xml"))); }
} else {
catch (IOException e) { if (is_snapshot) {
var meta_data = "";
try { 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) { var local_matcher = META_DATA_LOCAL_COPY.matcher(meta_data);
throw e; is_local = local_matcher.find();
} if (!is_local) {
} var version_matcher = META_DATA_SNAPSHOT_VERSION.matcher(meta_data);
var local_matcher = META_DATA_LOCAL_COPY.matcher(meta_data); if (version_matcher.find()) {
is_local = local_matcher.find(); download_version = version_matcher.group(1);
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_file = new File(DISTRIBUTIONS_DIR, bldFileName(version));
var distribution_sources_file = new File(DISTRIBUTIONS_DIR, bldSourcesFileName(version)); var distribution_sources_file = new File(DISTRIBUTIONS_DIR, bldSourcesFileName(version));
// if this is a snapshot and the distribution file exists, if (!offline_) {
// ensure that it's the latest by comparing hashes // if this is a snapshot and the distribution file exists,
if (is_snapshot && distribution_file.exists()) { // ensure that it's the latest by comparing hashes
boolean delete_distribution_files = is_local; if (is_snapshot && distribution_file.exists()) {
if (!delete_distribution_files) { boolean delete_distribution_files = is_local;
var download_md5 = readString(version, new URL(downloadUrl(version, bldFileName(download_version)) + ".md5")); if (!delete_distribution_files) {
try { var download_md5 = readString(version, new URL(downloadUrl(version, bldFileName(download_version)) + ".md5"));
var digest = MessageDigest.getInstance("MD5"); try {
digest.update(FileUtils.readBytes(distribution_file)); var digest = MessageDigest.getInstance("MD5");
if (!download_md5.equals(encodeHexLower(digest.digest()))) { digest.update(FileUtils.readBytes(distribution_file));
delete_distribution_files = true; 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) { // download distribution jars if necessary
distribution_file.delete(); if (!distribution_file.exists()) {
if (distribution_sources_file.exists()) { downloadDistribution(distribution_file, downloadUrl(version, bldFileName(download_version)));
distribution_sources_file.delete();
}
} }
if (!distribution_sources_file.exists()) {
} try {
downloadDistribution(distribution_sources_file, downloadUrl(version, bldSourcesFileName(download_version)));
// download distribution jars if necessary } catch (IOException e) {
if (!distribution_file.exists()) { // this is not critical, ignore
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
} }
} }
@ -618,7 +630,8 @@ public class Wrapper {
private void resolveExtensions() { private void resolveExtensions() {
if (null == classloader_ || if (null == classloader_ ||
null == wrapperPropertiesFile_) { null == wrapperPropertiesFile_ ||
offline_) {
return; return;
} }