Skip to content
Permalink
Browse files
shell - allowing to fallback on maven deps installation if no md.rest…
…x.json is found in current module
  • Loading branch information
fcamblor committed Aug 27, 2017
1 parent 0e769e6 commit 15e874d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
@@ -11,6 +11,7 @@
import org.apache.ivy.Ivy;
import org.apache.ivy.core.report.ResolveReport;
import org.apache.ivy.core.retrieve.RetrieveOptions;
import restx.AppSettings;
import restx.build.*;
import restx.build.ModuleDescriptor;
import restx.factory.Component;
@@ -19,10 +20,12 @@

import java.io.*;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.Arrays.asList;
@@ -82,6 +85,9 @@ public void run(RestxShell shell) throws Exception {
if(ModuleDescriptorType.RESTX.equals(moduleDescriptorTypeWithExistingFile)) {
shell.println("installing deps using restx module descriptor...");
installDepsFromModuleDescriptor(shell, ModuleDescriptorType.RESTX.resolveDescriptorFile(shell.currentLocation()));
} else if(ModuleDescriptorType.MAVEN.equals(moduleDescriptorTypeWithExistingFile)) {
shell.println("installing deps using maven descriptor...");
installDepsFromMavenDescriptor(shell, ModuleDescriptorType.MAVEN.resolveDescriptorFile(shell.currentLocation()));
} else {
throw new IllegalArgumentException("Unsupported deps install for module type "+moduleDescriptorTypeWithExistingFile);
}
@@ -115,6 +121,43 @@ private void installDepsFromModuleDescriptor(RestxShell shell, File mdFile) thro
}
}

private void installDepsFromMavenDescriptor(RestxShell shell, File pomFile) throws Exception {
AppSettings appSettings = shell.getFactory().getComponent(AppSettings.class);

Path dependenciesDir = Paths.get(appSettings.targetDependency());

// Emptying target dependencies directory first
if(dependenciesDir.toFile().exists()) {
java.nio.file.Files.walkFileTree(dependenciesDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
java.nio.file.Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
java.nio.file.Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}

dependenciesDir.toFile().mkdirs();

// Then copying dependencies through copy-dependencies plugin
ProcessBuilder mavenCmd = new ProcessBuilder(
"mvn", "org.apache.maven.plugins:maven-dependency-plugin:3.0.1:copy-dependencies",
"-DoutputDirectory=" + dependenciesDir.toAbsolutePath(), "-DincludeScope=runtime"
);

shell.println("Executing `"+mavenCmd+"` ...");
mavenCmd.redirectErrorStream(true)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.directory(shell.currentLocation().toFile().getAbsoluteFile())
.start()
.waitFor();
}

private static void storeModuleDescriptorMD5File(RestxShell shell, ModuleDescriptorType mdType) throws IOException {
File mdFile = mdType.resolveDescriptorFile(shell.currentLocation());
File md5File = ModuleDescriptorType.MAVEN.resolveDescriptorMd5File(shell.currentLocation());
@@ -31,7 +31,7 @@ public File resolveDescriptorMd5File(Path inDirectory) {

public static Optional<ModuleDescriptorType> firstModuleDescriptorTypeWithExistingFile(final Path inDirectory) {
return FluentIterable
.from(Arrays.asList(ModuleDescriptorType.RESTX))
.from(Arrays.asList(ModuleDescriptorType.RESTX, ModuleDescriptorType.MAVEN))
.filter(new Predicate<ModuleDescriptorType>() {
@Override
public boolean apply(ModuleDescriptorType mdType) {
@@ -1,11 +1,13 @@
## deps install

Resolve the dependencies declared in the file `md.restx.json`, and synchronize them in `target/dependency` directory.
Resolve dependencies declared in current module descriptor (in order: either `md.restx.json` or `pom.xml`),
and synchronize them in `target/dependency` directory.

This command uses Apache Ivy under the hood to perform dependency resolution, using settings provided by restx.
For `md.restx.json` `module.ivy`, this command uses Apache Ivy under the hood to perform dependency resolution,
using settings provided by restx. You can override these Ivy settings by placing a file named ivysettings.xml in your
restx shell install location (usually `~/.restx`).

You can override these Ivy settings by placing a file named ivysettings.xml in your restx shell install location
(usually `~/.restx`).
For `pom.xml`, this command will use maven-dependency-plugin:copy-dependencies goal to perform dependency resolution.

Note: You need to be placed in a restx app root directory to run this command.

0 comments on commit 15e874d

Please sign in to comment.