1. /*
  2. * Copyright 2000,2002,2004 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.optional.jlink;
  18. import java.io.File;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.taskdefs.MatchingTask;
  22. import org.apache.tools.ant.types.Path;
  23. /**
  24. * This class defines objects that can link together various jar and
  25. * zip files.
  26. *
  27. * <p>It is basically a wrapper for the jlink code written originally
  28. * by <a href="mailto:beard@netscape.com">Patrick Beard</a>. The
  29. * classes org.apache.tools.ant.taskdefs.optional.jlink.Jlink and
  30. * org.apache.tools.ant.taskdefs.optional.jlink.ClassNameReader
  31. * support this class.</p>
  32. *
  33. * <p>For example:
  34. * <code>
  35. * <pre>
  36. * <jlink compress="false" outfile="out.jar"/>
  37. * <mergefiles>
  38. * <pathelement path="${build.dir}/mergefoo.jar"/>
  39. * <pathelement path="${build.dir}/mergebar.jar"/>
  40. * </mergefiles>
  41. * <addfiles>
  42. * <pathelement path="${build.dir}/mac.jar"/>
  43. * <pathelement path="${build.dir}/pc.zip"/>
  44. * </addfiles>
  45. * </jlink>
  46. * </pre>
  47. * </code>
  48. *
  49. * @ant.task ignore="true"
  50. */
  51. public class JlinkTask extends MatchingTask {
  52. /**
  53. * The output file for this run of jlink. Usually a jar or zip file.
  54. */
  55. public void setOutfile(File outfile) {
  56. this.outfile = outfile;
  57. }
  58. /**
  59. * Establishes the object that contains the files to
  60. * be merged into the output.
  61. */
  62. public Path createMergefiles() {
  63. if (this.mergefiles == null) {
  64. this.mergefiles = new Path(getProject());
  65. }
  66. return this.mergefiles.createPath();
  67. }
  68. /**
  69. * Sets the files to be merged into the output.
  70. */
  71. public void setMergefiles(Path mergefiles) {
  72. if (this.mergefiles == null) {
  73. this.mergefiles = mergefiles;
  74. } else {
  75. this.mergefiles.append(mergefiles);
  76. }
  77. }
  78. /**
  79. * Establishes the object that contains the files to
  80. * be added to the output.
  81. */
  82. public Path createAddfiles() {
  83. if (this.addfiles == null) {
  84. this.addfiles = new Path(getProject());
  85. }
  86. return this.addfiles.createPath();
  87. }
  88. /**
  89. * Sets the files to be added into the output.
  90. */
  91. public void setAddfiles(Path addfiles) {
  92. if (this.addfiles == null) {
  93. this.addfiles = addfiles;
  94. } else {
  95. this.addfiles.append(addfiles);
  96. }
  97. }
  98. /**
  99. * Defines whether or not the output should be compacted.
  100. */
  101. public void setCompress(boolean compress) {
  102. this.compress = compress;
  103. }
  104. /**
  105. * Does the adding and merging.
  106. */
  107. public void execute() throws BuildException {
  108. //Be sure everything has been set.
  109. if (outfile == null) {
  110. throw new BuildException("outfile attribute is required! "
  111. + "Please set.");
  112. }
  113. if (!haveAddFiles() && !haveMergeFiles()) {
  114. throw new BuildException("addfiles or mergefiles required! "
  115. + "Please set.");
  116. }
  117. log("linking: " + outfile.getPath());
  118. log("compression: " + compress, Project.MSG_VERBOSE);
  119. jlink linker = new jlink();
  120. linker.setOutfile(outfile.getPath());
  121. linker.setCompression(compress);
  122. if (haveMergeFiles()) {
  123. log("merge files: " + mergefiles.toString(), Project.MSG_VERBOSE);
  124. linker.addMergeFiles(mergefiles.list());
  125. }
  126. if (haveAddFiles()) {
  127. log("add files: " + addfiles.toString(), Project.MSG_VERBOSE);
  128. linker.addAddFiles(addfiles.list());
  129. }
  130. try {
  131. linker.link();
  132. } catch (Exception ex) {
  133. throw new BuildException(ex, getLocation());
  134. }
  135. }
  136. private boolean haveAddFiles() {
  137. return haveEntries(addfiles);
  138. }
  139. private boolean haveMergeFiles() {
  140. return haveEntries(mergefiles);
  141. }
  142. private boolean haveEntries(Path p) {
  143. if (p == null) {
  144. return false;
  145. }
  146. if (p.size() > 0) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. private File outfile = null;
  152. private Path mergefiles = null;
  153. private Path addfiles = null;
  154. private boolean compress = false;
  155. }