Skip to content

Commit

Permalink
[#199] Load classes from dependency jars when extracting s-pipes modu…
Browse files Browse the repository at this point in the history
…le metadata from maven modules.

- uncomment the plugin execution in s-pipes-module/pom.xml
  • Loading branch information
kostobog committed Aug 29, 2023
1 parent 0f8828d commit 82688e5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDFS;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -171,9 +176,8 @@ private void generateRdfForModule() throws MojoExecutionException {
//region Parsing Java classes
private Set<Class<?>> readAllModuleClasses(MavenProject project) throws MalformedURLException, ClassNotFoundException {
//Configure the class searcher
final File classesDirectory = new File(project.getBuild().getOutputDirectory());
final URL classesUrl = classesDirectory.toURI().toURL();
final URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{classesUrl}, getClass().getClassLoader());
final URL[] classesUrls = getDependencyURLs(project);
final URLClassLoader classLoader = URLClassLoader.newInstance(classesUrls, getClass().getClassLoader());
var reflectionConfig = new ConfigurationBuilder()
.setUrls(ClasspathHelper.forClassLoader(classLoader))
.setScanners(new SubTypesScanner(false), new TypeAnnotationsScanner())
Expand All @@ -199,6 +203,37 @@ private Set<Class<?>> readAllModuleClasses(MavenProject project) throws Malforme
return moduleClasses;
}

private URL[] getDependencyURLs(MavenProject project) throws MalformedURLException {
Set<URL> ret = new HashSet<>();
ret.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
for(Dependency d : project.getDependencies()){
URL dURL = getURL(getLocalRepository(project), d);
ret.add(dURL);
}
return ret.toArray(new URL[]{});
}

/**
* @implNote use of deprecated API to
* @param project
* @return instance of local repository
*/
private ArtifactRepository getLocalRepository(MavenProject project){
return project.getProjectBuildingRequest().getLocalRepository();
}

private URL getURL(ArtifactRepository r, Dependency d) throws MalformedURLException {
return new File(r.getBasedir(), r.pathOf(toArtifact(d)))
.toURI().toURL();
}

private Artifact toArtifact(Dependency d){
DefaultArtifactHandler h = new DefaultArtifactHandler(d.getType());
return new DefaultArtifact(
d.getGroupId(), d.getArtifactId(), d.getVersion(), d.getScope(), d.getType(),null, h
);
}

private SPipesModule readModuleAnnotationFromClass(Class<?> classObject) {
return classObject.getAnnotation(MODULE_ANNOTATION);
}
Expand Down
36 changes: 18 additions & 18 deletions s-pipes-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@
</configuration>
</plugin>

<!-- <plugin>-->
<!-- <groupId>cz.cvut.kbss</groupId>-->
<!-- <artifactId>s-pipes-module-creator-maven-plugin</artifactId>-->
<!-- <version>${project.parent.version}</version>-->
<!-- <inherited>false</inherited>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <goals>-->
<!-- <goal>process-annotations</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <mode>RDF_FOR_ALL_CHILDREN</mode>-->
<!-- <modulePackageName>cz.cvut.spipes</modulePackageName>-->
<!-- <ontologyFilename>all-modules.ttl</ontologyFilename>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>cz.cvut.kbss</groupId>
<artifactId>s-pipes-module-creator-maven-plugin</artifactId>
<version>${project.parent.version}</version>
<inherited>false</inherited>
<executions>
<execution>
<goals>
<goal>process-annotations</goal>
</goals>
<configuration>
<mode>RDF_FOR_ALL_CHILDREN</mode>
<modulePackageName>cz.cvut.spipes</modulePackageName>
<ontologyFilename>all-modules.ttl</ontologyFilename>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
Expand Down

0 comments on commit 82688e5

Please sign in to comment.