/**
* Given a JAR file, get the names of any auto-loadable Java 5-style or Java 6-style annotation processor implementations provided by the JAR. The information is based on the Sun <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider"> Jar Service Provider spec</a>: the jar file contains a META-INF/services directory; that directory contains text files named according to the desired interfaces; and each file contains the names of the classes implementing the specified service. The files may also contain whitespace (which is to be ignored). The '#' character indicates the beginning of a line comment, also to be ignored. Implied but not stated in the spec is that this routine also ignores anything after the first nonwhitespace token on a line.
* @param jar the <code>.jar</code> {@link File} to inspect for annotation processor services
* @return the {@link Set} of auto-loadable Java 5-style or Java 6-style annotation processor {@link ServiceEntry}s provided by the specified JAR, or an empty {@link Set} if no such {@link ServiceEntry}s are found
*/
public static Set<ServiceEntry> getAptServiceEntries(File jar) throws IOException {
if (jar == null) throw new IllegalArgumentException(String.format("Null %s.",File.class));
if (!jar.exists()) throw new IllegalArgumentException(String.format("Specified file does not exist: %s",jar.getAbsolutePath()));
if (!jar.canRead()) throw new IllegalArgumentException(String.format("Specified file not readable: %s",jar.getAbsolutePath()));
Set<ServiceEntry> serviceEntries=new HashSet<ServiceEntry>();
JarFile jarFile=null;
try {
jarFile=new JarFile(jar);
for ( String serviceName : APT_SERVICES) {
String providerName="META-INF/services/" + serviceName;
JarEntry provider=jarFile.getJarEntry(providerName);
if (provider == null) {
continue;
}
InputStream is=jarFile.getInputStream(provider);
Set<ServiceEntry> serviceFileEntries=readServiceProvider(serviceName,is);
serviceEntries.addAll(serviceFileEntries);
}
return serviceEntries;
}
finally {
try {
if (jarFile != null) jarFile.close();
}
catch ( IOException ioe) {
}
}
}

Example 64

From project m2eclipse-extras, under directory /org.sonatype.m2e.discovery.publisher.maven-plugin/src/main/java/org/sonatype/m2e/discovery/publisher/.