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,8 +485,13 @@ public class BaseProject extends BuildExecutor {
@BuildCommand(value = "dependency-tree", help = DependencyTreeHelp.class)
public void dependencyTree()
throws Exception {
if (isOffline()) {
System.out.println("Offline mode: dependency-tree is disabled");
}
else {
dependencyTreeOperation().executeOnce(() -> dependencyTreeOperation().fromProject(this));
}
}
/**
* Standard build command, downloads all dependencies of the project.
@ -496,8 +501,13 @@ public class BaseProject extends BuildExecutor {
@BuildCommand(help = DownloadHelp.class)
public void download()
throws Exception {
if (isOffline()) {
System.out.println("Offline mode: download is disabled");
}
else {
downloadOperation().executeOnce(() -> downloadOperation().fromProject(this));
}
}
/**
* Standard build command, purges all unused artifacts from the project.
@ -507,8 +517,13 @@ public class BaseProject extends BuildExecutor {
@BuildCommand(help = PurgeHelp.class)
public void purge()
throws Exception {
if (isOffline()) {
System.out.println("Offline mode: purge is disabled");
}
else {
purgeOperation().executeOnce(() -> purgeOperation().fromProject(this));
}
}
/**
* Standard build command, runs the project.
@ -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,8 +191,13 @@ public class Project extends BaseProject {
jar();
jarSources();
jarJavadoc();
if (isOffline()) {
System.out.println("Offline mode: publish is disabled");
}
else {
publishOperation().executeOnce(() -> publishOperation().fromProject(this));
}
}
/**
* Standard publish-local command, transfers artifacts to the local maven repository.

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,16 +519,19 @@ public class Wrapper {
var download_version = version;
var is_snapshot = isSnapshot(version);
var is_local = false;
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.xml")));
}
catch (IOException e) {
} catch (IOException e) {
try {
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata-local.xml")));
}
catch (IOException e2) {
} catch (IOException e2) {
throw e;
}
}
@ -535,10 +544,12 @@ public class Wrapper {
}
}
}
}
var distribution_file = new File(DISTRIBUTIONS_DIR, bldFileName(version));
var distribution_sources_file = new File(DISTRIBUTIONS_DIR, bldSourcesFileName(version));
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()) {
@ -575,6 +586,7 @@ public class Wrapper {
// this is not critical, ignore
}
}
}
// find the wrapper classloader in the hierarchy and add the bld jar to it
classloader_ = new WrapperClassLoader();
@ -618,7 +630,8 @@ public class Wrapper {
private void resolveExtensions() {
if (null == classloader_ ||
null == wrapperPropertiesFile_) {
null == wrapperPropertiesFile_ ||
offline_) {
return;
}