| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.jjar; |
| 18 | |
|
| 19 | |
import java.net.URL; |
| 20 | |
import java.net.URLConnection; |
| 21 | |
import java.util.List; |
| 22 | |
import java.util.Iterator; |
| 23 | |
|
| 24 | |
import org.apache.tools.ant.BuildException; |
| 25 | |
import org.apache.tools.ant.Task; |
| 26 | |
import org.apache.tools.ant.types.Path; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | 0 | public class JJARTask extends Task |
| 34 | |
{ |
| 35 | 0 | private static String defaultRepository = ClasspathUtil.getDefaultRepository(); |
| 36 | 0 | private String pkg = null; |
| 37 | 0 | private String ver = null; |
| 38 | 0 | private String localrep = "."; |
| 39 | 0 | private boolean verifyignore = true; |
| 40 | 0 | private boolean onlydependencies = false; |
| 41 | 0 | private String classpath = null; |
| 42 | 0 | private String pathrefid = null; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public void setPackage( String pkg) |
| 49 | |
{ |
| 50 | 0 | this.pkg = pkg; |
| 51 | 0 | } |
| 52 | |
|
| 53 | |
public void setVersion( String version ) |
| 54 | |
{ |
| 55 | 0 | this.ver = version; |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
public void setLocalrepository( String localrep ) |
| 59 | |
{ |
| 60 | 0 | this.localrep = localrep; |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
public void setVerifyignore( boolean b ) |
| 64 | |
{ |
| 65 | 0 | this.verifyignore = b; |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public void setOnlyDependencies( boolean b ) |
| 69 | |
{ |
| 70 | 0 | this.onlydependencies = b; |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
public void setRepository( String rep ) |
| 74 | |
{ |
| 75 | 0 | this.defaultRepository = rep; |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
public void setClasspath( String classpath ) |
| 79 | |
{ |
| 80 | 0 | this.classpath = classpath; |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
public void setPathrefid( String pathrefid ) |
| 84 | |
{ |
| 85 | 0 | this.pathrefid = pathrefid; |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public void execute () |
| 96 | |
throws BuildException |
| 97 | |
{ |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 0 | if( pkg == null) |
| 102 | 0 | throw new BuildException("Need to specify a package to fetch"); |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | 0 | Repository repository = null; |
| 108 | |
|
| 109 | |
try |
| 110 | |
{ |
| 111 | 0 | repository = new RepositoryXML(); |
| 112 | 0 | repository.load( new URL( defaultRepository + "repository.xml") ); |
| 113 | |
|
| 114 | |
} |
| 115 | 0 | catch( Exception e ) |
| 116 | |
{ |
| 117 | 0 | throw new BuildException("Problem getting repository info", e ); |
| 118 | 0 | } |
| 119 | |
|
| 120 | 0 | log("Fetching package '" + pkg + "' from repository '" + defaultRepository |
| 121 | |
+ "' into local repository '" + localrep + "'"); |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | 0 | Path antpath = new Path( project ); |
| 128 | |
|
| 129 | |
try |
| 130 | |
{ |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | 0 | if( ver == null) |
| 136 | 0 | ver = repository.getPackageDefaultVersion( pkg ); |
| 137 | |
|
| 138 | 0 | List deplist = repository.getDependencyList( pkg, ver ); |
| 139 | |
|
| 140 | 0 | Iterator ii = deplist.iterator(); |
| 141 | |
|
| 142 | 0 | while(ii.hasNext()) |
| 143 | |
{ |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | 0 | String depnameuni = (String) ii.next(); |
| 149 | |
|
| 150 | 0 | JJARPackage jjarp = new JJARPackage( depnameuni ); |
| 151 | |
|
| 152 | 0 | String depname = jjarp.getName(); |
| 153 | 0 | String depver = jjarp.getVersionString(); |
| 154 | 0 | String deptarget = repository.getFetchTarget( depname, depver ); |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | 0 | if ( verifyignore && ClasspathUtil.containsVersionedJar( classpath, depname, depver)) |
| 161 | |
{ |
| 162 | 0 | log(" Dependency '" + depname + "' already in classpath - skipping"); |
| 163 | 0 | continue; |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | 0 | URL url = new URL( defaultRepository + deptarget ); |
| 171 | 0 | URLConnection uconn = (URLConnection) url.openConnection(); |
| 172 | |
|
| 173 | 0 | log(" Fetching dependency : " + deptarget ); |
| 174 | |
|
| 175 | 0 | Transport.fetchJar( uconn, localrep + "/" + deptarget ); |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | 0 | log("Adding depedency element " + localrep + "/" + deptarget + " to classpath"); |
| 183 | |
|
| 184 | 0 | antpath.createPathElement().setPath( localrep + "/" + deptarget ); |
| 185 | 0 | } |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | 0 | if( onlydependencies ) |
| 194 | |
{ |
| 195 | 0 | log(" Dependency-only mode : not fetching target " + pkg ); |
| 196 | |
return; |
| 197 | |
} |
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | 0 | if (ver == null) |
| 204 | |
{ |
| 205 | 0 | ver = repository.getPackageDefaultVersion( pkg ); |
| 206 | |
} |
| 207 | |
|
| 208 | 0 | if( verifyignore && ClasspathUtil.containsVersionedJar( classpath, pkg, ver)) |
| 209 | |
{ |
| 210 | 0 | log(" Skipping fetch of '" + pkg + "' - already exists in classpath"); |
| 211 | |
|
| 212 | |
} |
| 213 | |
else |
| 214 | |
{ |
| 215 | 0 | String fetchTarget = repository.getFetchTarget( pkg, ver ); |
| 216 | |
|
| 217 | 0 | URL url = new URL( defaultRepository + fetchTarget ); |
| 218 | 0 | URLConnection uconn = (URLConnection) url.openConnection(); |
| 219 | |
|
| 220 | 0 | log(" Fetching package : " + pkg ); |
| 221 | |
|
| 222 | 0 | Transport.fetchJar( uconn, localrep + "/" + fetchTarget ); |
| 223 | |
|
| 224 | 0 | log("Adding package element " + localrep + "/" + fetchTarget + " to classpath"); |
| 225 | |
|
| 226 | 0 | antpath.createPathElement().setPath( localrep + "/" + fetchTarget ); |
| 227 | |
|
| 228 | |
} |
| 229 | |
} |
| 230 | 0 | catch( Exception e ) |
| 231 | |
{ |
| 232 | 0 | throw new BuildException("Exception : ",e); |
| 233 | |
} |
| 234 | |
finally |
| 235 | |
{ |
| 236 | 0 | if( pathrefid != null) |
| 237 | |
{ |
| 238 | 0 | project.addReference( pathrefid, antpath); |
| 239 | |
} |
| 240 | |
} |
| 241 | 0 | } |
| 242 | |
|
| 243 | |
|
| 244 | |
} |