diff --git a/WHATSNEW b/WHATSNEW index 9dbe31342..75b440491 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -132,6 +132,11 @@ Other changes: instances when overriding other methods like runStatements. Bugzilla Report 27178. + * has a new failOnConnectionError attribute that can be used to + keep a build going even if the task failed to connect to the + database. + Bugzilla Report 36712. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTasks/sql.html b/docs/manual/CoreTasks/sql.html index b348c63c8..db9b8ff05 100644 --- a/docs/manual/CoreTasks/sql.html +++ b/docs/manual/CoreTasks/sql.html @@ -195,6 +195,13 @@

Parameters

No, default false + + failOnConnectionError + If false, will only print a warning + message and not execute any statement if the task fails to connect + to the database. Since Ant 1.8.0. + No, default true +

Parameters specified as nested elements

diff --git a/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java b/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java index 1a0e138f9..974c3e4bd 100644 --- a/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/JDBCTask.java @@ -142,6 +142,12 @@ public abstract class JDBCTask extends Task { **/ private String version = null; + /** + * whether the task fails when ant fails to connect to the database. + * @since Ant 1.8.0 + */ + private boolean failOnConnectionError = true; + /** * Sets the classpath for loading the driver. * @param classpath The classpath to set @@ -231,6 +237,15 @@ public abstract class JDBCTask extends Task { this.version = version; } + /** + * whether the task should cause the build to fail if it cannot + * connect to the database. + * @since Ant 1.8.0 + */ + public void setFailOnConnectionError(boolean b) { + failOnConnectionError = b; + } + /** * Verify we are connected to the correct RDBMS * @param conn the jdbc connection @@ -296,7 +311,8 @@ public abstract class JDBCTask extends Task { * * The calling method is responsible for closing the connection. * - * @return Connection the newly created connection. + * @return Connection the newly created connection or null if the + * connection failed and failOnConnectionError is false. * @throws BuildException if the UserId/Password/Url is not set or there * is no suitable driver or the driver fails to load. */ @@ -326,7 +342,13 @@ public abstract class JDBCTask extends Task { conn.setAutoCommit(autocommit); return conn; } catch (SQLException e) { - throw new BuildException(e, getLocation()); + // failed to connect + if (!failOnConnectionError) { + log("Failed to connect: " + e.getMessage(), Project.MSG_WARN); + return null; + } else { + throw new BuildException(e, getLocation()); + } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index 03a50fec7..cb0f0130f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -159,7 +159,7 @@ public class SQLExec extends JDBCTask { /** * Action to perform if an error is found - **/ + */ private String onError = "abort"; /**