Maven
Nasdanika Maven module uses Maven Resolver Supplier to provide capabilities for Dependency and ClassLoader requirements.
Dependency
CapabilityLoader capabilityLoader = new CapabilityLoader();
DependencyRequestRecord requirement = new DependencyRequestRecord(
new String[] { "org.apache.groovy:groovy-all:pom:4.0.23" },
null,
null,
"target/test-repo");
ProgressMonitor progressMonitor = new PrintStreamProgressMonitor();
Collection<File> result = capabilityLoader.loadOne(requirement, progressMonitor);
The above code snippet loads org.apache.groovy:groovy-all:pom:4.0.23 and its dependencies into target/test-repo directory and returns a list of jar files.
ClassLoader
CapabilityLoader capabilityLoader = new CapabilityLoader();
ClassLoaderRequirement requirement = new ClassLoaderRequirement(
null, // String[] modulePath,
null, // String[] rootModules,
new ModuleLayer[] { getClass().getModule().getLayer() },
getClass().getClassLoader(), // ClassLoader parentClassLoader,
true, // boolean singleLayerClassLoader,
new String[] { "org.apache.groovy:groovy-all:pom:4.0.23" },
null,
null,
"target/test-repo",
System.out::println);
ProgressMonitor progressMonitor = new PrintStreamProgressMonitor();
ClassLoader result = capabilityLoader.loadOne(
ServiceCapabilityFactory.createRequirement(ClassLoader.class, null, requirement),
progressMonitor);
Class<?> scriptEngineFactoryClass = result.loadClass("org.codehaus.groovy.jsr223.GroovyScriptEngineFactory");
The above code snippet:
- Loads org.apache.groovy:groovy-all:pom:4.0.23 and its dependencies into
target/test-repodirectory - Creates a ClassLoader
- Loads org.codehaus.groovy.jsr223.GroovyScriptEngineFactory class
Default configuration
DependencyCapabilityFactory, which is responsible for resolving and downloading dependencies, loads default configuration in the following way:
- If
org.nasdanika.maven.DependencyCapabilityFactory.config.ymlsystem property is set, then it is treated as a URL of a configuration YAML resource. The URL is resolved relative to the current directory. - If
org.nasdanika.maven.DependencyCapabilityFactory.config.jsonsystem property is set, then it is treated as a URL of a configuration JSON resource. The URL is resolved relative to the current directory. - If
NSD_DEPENDENCY_RESOLVER_CONFIG_YAML_URLenvironment variable is set, then it is treated as an absolute URL of a configuration YAML resource. - If
NSD_DEPENDENCY_RESOLVER_CONFIG_JSON_URLenvironment variable is set, then it is treated as an absolute URL of a configuration JSON resource. - If
NSD_DEPENDENCY_RESOLVER_CONFIG_YAMLenvironment variable is set, then it is treated as YAML configuration. - If
NSD_DEPENDENCY_RESOLVER_CONFIG_JSONenvironment variable is set, then it is treated as JSON configuration. - If
dependency-reolver-config.ymlfile exists in the current directory the it is loaded as YAML. - If
dependency-reolver-config.jsonfile exists in the current directory the it is loaded as JSON.
The loaded configuration is interpolated with system properties and environment variables. E.g. ${my-property} will be expanded to the value of my-property system property if it is set. Respectively, ${env.MY_ENV_VAR} will be expanded to the value of MY_ENV_VAR environment variable if it is set. Property expansion can be escaped with additional {} e.g. ${my-property} will be expanded to ${my-property} regardless of whether my-properety system property is set or not.
Configuration specification
modulePath- optional String or List, module path. If null, derived from root modules if they are presentrootModules- optional String or List, root modules. The first root module is used to obtain the class loaderoneLayerClassLoader- optional boolean indicating whether a single class loader shall be used for all modules in in the layerdependencies- optional String or List of dependencies in<groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}format. E.g.org.apache.groovy:groovy-all:pom:4.0.23managedDependencies- optional String or List of dependencies in<groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}formatremoteRepositories- Map (single remote repository) or List of Maps of remote repository definitions loaded into RemoteRepoRecord:id- String, repo IDtype- String, optional repo typeurl- String, repository URLproxy- optional Map:type- String,httporhttpshost- Stringport- integerauth- authentication (see below)
auth- Map:username- Stringpassword- String
mirroredRepositories- Map or List, mirrored repositories
localRepository- optional String, path to the local repository to download dependencies to. Defaults torepository.
URI Handler
Maven URIHandler handles URIs of the following format: maven://<groupId>/<artifactId>/<extension>/<version>/<resource path>[?classifier=<classifier>].
For example, for maven://org.nasdanika.models.architecture/model/jar/2024.8.0/model/architecture.ecore?classifier=model URI model/architecture.ecore resource would be loaded from model-2024.8.0-model.jar in org.nasdanika.models.architecture group model artifact version 2024.8.0 as shown in the snippet below:
CapabilityLoader capabilityLoader = new CapabilityLoader();
ProgressMonitor progressMonitor = new PrintStreamProgressMonitor();
Requirement<ResourceSetRequirement, ResourceSet> requirement = ServiceCapabilityFactory.createRequirement(ResourceSet.class);
ResourceSet resourceSet = capabilityLoader.loadOne(requirement, progressMonitor);
URI modeURI = URI.createURI("maven://org.nasdanika.models.architecture/model/jar/2024.8.0/model/architecture.ecore?classifier=model");
Resource resource = resourceSet.getResource(modeURI, true);
System.out.println(resource.getContents());
Nasdanika