You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConditionTask.java 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2001-2006 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Project;
  20. import org.apache.tools.ant.taskdefs.condition.Condition;
  21. import org.apache.tools.ant.taskdefs.condition.ConditionBase;
  22. /**
  23. * Task to set a property conditionally using <uptodate>, <available>,
  24. * and many other supported conditions.
  25. *
  26. * <p>This task supports boolean logic as well as pluggable conditions
  27. * to decide, whether a property should be set.</p>
  28. *
  29. * <p>This task does not extend Task to take advantage of
  30. * ConditionBase.</p>
  31. *
  32. *
  33. * @since Ant 1.4
  34. *
  35. * @ant.task category="control"
  36. */
  37. public class ConditionTask extends ConditionBase {
  38. private String property = null;
  39. private String value = "true";
  40. private String alternative = null;
  41. public ConditionTask() {
  42. super("condition");
  43. }
  44. /**
  45. * The name of the property to set. Required.
  46. * @param p the name of the property
  47. * @since Ant 1.4
  48. */
  49. public void setProperty(String p) {
  50. property = p;
  51. }
  52. /**
  53. * The value for the property to set, if condition evaluates to true.
  54. * Defaults to "true".
  55. * @param v the value of the property
  56. * @since Ant 1.4
  57. */
  58. public void setValue(String v) {
  59. value = v;
  60. }
  61. /**
  62. * The value for the property to set, if condition evaluates to false.
  63. * If this attribute is not specified, the property will not be set.
  64. * @param e the alternate value of the property.
  65. * @since Ant 1.6.3
  66. */
  67. public void setElse(String e) {
  68. alternative = e;
  69. }
  70. /**
  71. * See whether our nested condition holds and set the property.
  72. *
  73. * @since Ant 1.4
  74. * @exception BuildException if an error occurs
  75. */
  76. public void execute() throws BuildException {
  77. if (countConditions() > 1) {
  78. throw new BuildException("You must not nest more than one "
  79. + "condition into <"
  80. + getTaskName() + ">");
  81. }
  82. if (countConditions() < 1) {
  83. throw new BuildException("You must nest a condition into <"
  84. + getTaskName() + ">");
  85. }
  86. if (property == null) {
  87. throw new BuildException("The property attribute is required.");
  88. }
  89. Condition c = (Condition) getConditions().nextElement();
  90. if (c.eval()) {
  91. log("Condition true; setting " + property + " to " + value,
  92. Project.MSG_DEBUG);
  93. getProject().setNewProperty(property, value);
  94. } else if (alternative != null) {
  95. log("Condition false; setting " + property + " to " + alternative,
  96. Project.MSG_DEBUG);
  97. getProject().setNewProperty(property, alternative);
  98. } else {
  99. log("Condition false; not setting " + property,
  100. Project.MSG_DEBUG);
  101. }
  102. }
  103. }