Skip to content

Commit 15e874d

Browse files
committed
shell - allowing to fallback on maven deps installation if no md.restx.json is found in current module
1 parent 0e769e6 commit 15e874d

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

restx-core-shell/src/main/java/restx/core/shell/DepsShellCommand.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.ivy.Ivy;
1212
import org.apache.ivy.core.report.ResolveReport;
1313
import org.apache.ivy.core.retrieve.RetrieveOptions;
14+
import restx.AppSettings;
1415
import restx.build.*;
1516
import restx.build.ModuleDescriptor;
1617
import restx.factory.Component;
@@ -19,10 +20,12 @@
1920

2021
import java.io.*;
2122
import java.net.URL;
23+
import java.nio.file.FileVisitResult;
2224
import java.nio.file.Path;
2325
import java.nio.file.Paths;
26+
import java.nio.file.SimpleFileVisitor;
27+
import java.nio.file.attribute.BasicFileAttributes;
2428
import java.util.ArrayList;
25-
import java.util.Arrays;
2629
import java.util.List;
2730

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

124+
private void installDepsFromMavenDescriptor(RestxShell shell, File pomFile) throws Exception {
125+
AppSettings appSettings = shell.getFactory().getComponent(AppSettings.class);
126+
127+
Path dependenciesDir = Paths.get(appSettings.targetDependency());
128+
129+
// Emptying target dependencies directory first
130+
if(dependenciesDir.toFile().exists()) {
131+
java.nio.file.Files.walkFileTree(dependenciesDir, new SimpleFileVisitor<Path>() {
132+
@Override
133+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
134+
java.nio.file.Files.delete(file);
135+
return FileVisitResult.CONTINUE;
136+
}
137+
@Override
138+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
139+
java.nio.file.Files.delete(dir);
140+
return FileVisitResult.CONTINUE;
141+
}
142+
});
143+
}
144+
145+
dependenciesDir.toFile().mkdirs();
146+
147+
// Then copying dependencies through copy-dependencies plugin
148+
ProcessBuilder mavenCmd = new ProcessBuilder(
149+
"mvn", "org.apache.maven.plugins:maven-dependency-plugin:3.0.1:copy-dependencies",
150+
"-DoutputDirectory=" + dependenciesDir.toAbsolutePath(), "-DincludeScope=runtime"
151+
);
152+
153+
shell.println("Executing `"+mavenCmd+"` ...");
154+
mavenCmd.redirectErrorStream(true)
155+
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
156+
.directory(shell.currentLocation().toFile().getAbsoluteFile())
157+
.start()
158+
.waitFor();
159+
}
160+
118161
private static void storeModuleDescriptorMD5File(RestxShell shell, ModuleDescriptorType mdType) throws IOException {
119162
File mdFile = mdType.resolveDescriptorFile(shell.currentLocation());
120163
File md5File = ModuleDescriptorType.MAVEN.resolveDescriptorMd5File(shell.currentLocation());

restx-core-shell/src/main/java/restx/core/shell/ModuleDescriptorType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public File resolveDescriptorMd5File(Path inDirectory) {
3131

3232
public static Optional<ModuleDescriptorType> firstModuleDescriptorTypeWithExistingFile(final Path inDirectory) {
3333
return FluentIterable
34-
.from(Arrays.asList(ModuleDescriptorType.RESTX))
34+
.from(Arrays.asList(ModuleDescriptorType.RESTX, ModuleDescriptorType.MAVEN))
3535
.filter(new Predicate<ModuleDescriptorType>() {
3636
@Override
3737
public boolean apply(ModuleDescriptorType mdType) {

restx-core-shell/src/main/resources/restx/core/shell/deps.man

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
## deps install
22

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

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

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

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

0 commit comments

Comments
 (0)