Skip to content

Commit a0e7c4f

Browse files
authored
Merge pull request #12 from franden/generic_war_name
thanks for your contribution!
2 parents 620738b + a3745e5 commit a0e7c4f

File tree

6 files changed

+67
-31
lines changed

6 files changed

+67
-31
lines changed

src/main/java/com/airhacks/wad/boundary/WADFlow.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
1616
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
1717
import java.util.ArrayList;
18+
import java.util.Collection;
1819
import java.util.List;
1920
import java.util.LongSummaryStatistics;
2021
import java.util.concurrent.atomic.AtomicLong;
@@ -26,15 +27,15 @@
2627
* @author airhacks.com
2728
*/
2829
public class WADFlow {
29-
private final List<Long> buildTimes;
30+
private final Collection<Long> buildTimes;
3031

3132
private final AtomicLong successCounter = new AtomicLong();
3233
private final AtomicLong buildErrorCounter = new AtomicLong();
3334

3435
private final Copier copier;
3536
private final Builder builder;
3637

37-
public WADFlow(Path dir, Path war, List<Path> deploymentTargets) throws IOException {
38+
public WADFlow(Path dir, Path war, Collection<Path> deploymentTargets) throws IOException {
3839
this.builder = new Builder();
3940
this.buildTimes = new ArrayList<>();
4041
this.copier = new Copier(war, deploymentTargets);

src/main/java/com/airhacks/wad/control/Copier.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
import java.nio.file.Path;
77
import java.nio.file.StandardCopyOption;
88
import java.util.ArrayList;
9+
import java.util.Collection;
910
import java.util.List;
1011
import java.util.LongSummaryStatistics;
12+
import java.util.Optional;
13+
import java.util.stream.Collectors;
14+
import java.util.stream.Stream;
1115

1216
/**
1317
*
@@ -16,17 +20,15 @@
1620
public class Copier {
1721

1822
private List<Long> warSizes;
19-
private final List<Path> deploymentTargets;
20-
private final Path from;
23+
private final Collection<Path> deploymentTargetDirectories;
24+
private final Path fromDirectory;
2125

22-
public Copier(Path from, List<Path> deploymentTargets) {
26+
public Copier(Path from, Collection<Path> deploymentTargetDirectories) {
2327
this.warSizes = new ArrayList<>();
24-
this.from = from;
25-
this.deploymentTargets = deploymentTargets;
28+
this.fromDirectory = from;
29+
this.deploymentTargetDirectories = deploymentTargetDirectories;
2630
}
2731

28-
29-
3032
String shortenForDisplay(Path path, int maxLength) {
3133
String message = path.toString();
3234
int length = message.length();
@@ -38,15 +40,36 @@ String shortenForDisplay(Path path, int maxLength) {
3840
}
3941

4042
public void copy() {
41-
deploymentTargets.forEach(target -> copySingle(this.from, target));
43+
Path warPath = findWarInPath(this.fromDirectory).orElseThrow(() -> new IllegalStateException("war file was not found in the path"));
44+
List<Path> copyTargets = addWarName(deploymentTargetDirectories, warPath.getFileName().toString());
45+
copyTargets.forEach(target -> copySingle(warPath, target));
46+
}
47+
48+
Optional<Path> findWarInPath(Path fromDirectory) {
49+
try (Stream<Path> stream = Files.walk(fromDirectory, 1)) {
50+
return stream
51+
.filter(file -> !Files.isDirectory(file) && file.getFileName().toString().endsWith(".war"))
52+
.findFirst();
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
}
56+
57+
return null;
58+
}
59+
60+
List<Path> addWarName(Collection<Path> deploymentDirectories, String warName) {
61+
return deploymentDirectories.
62+
stream().
63+
map(path -> path.resolve(warName)).
64+
collect(Collectors.toList());
4265
}
4366

4467
Path copySingle(Path from, Path to) {
4568
long kb;
4669
try {
4770
kb = Files.size(from) / 1024;
4871
warSizes.add(kb);
49-
System.out.printf("Copying %dkB ThinWAR to %s %s %s \n", kb, TerminalColors.FILE.value(), shortenForDisplay(to, 40), TerminalColors.RESET.value());
72+
System.out.printf("Copying %dkB ThinWAR %s to %s %s %s \n", kb, from.toString(), TerminalColors.FILE.value(), shortenForDisplay(to, 40), TerminalColors.RESET.value());
5073
return Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
5174

5275
} catch (IOException ex) {

src/main/java/wad/App.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
package wad;
33

4-
import com.airhacks.wad.boundary.WADFlow;
5-
import com.airhacks.wad.control.Configurator;
64
import static com.airhacks.wad.control.PreBuildChecks.pomExists;
75
import static com.airhacks.wad.control.PreBuildChecks.validateDeploymentDirectories;
6+
7+
import com.airhacks.wad.boundary.WADFlow;
8+
import com.airhacks.wad.control.Configurator;
89
import java.io.File;
910
import java.io.IOException;
1011
import java.io.InputStream;
@@ -45,12 +46,6 @@ static List<Path> convert(String[] args) {
4546
return Arrays.stream(args).map(App::addTrailingSlash).collect(Collectors.toList());
4647
}
4748

48-
static List<Path> addWarName(Set<Path> deploymentDirectories, String warName) {
49-
return deploymentDirectories.
50-
stream().
51-
map(path -> path.resolve(warName)).
52-
collect(Collectors.toList());
53-
}
5449

5550

5651
public static void main(String[] args) throws IOException {
@@ -60,19 +55,15 @@ public static void main(String[] args) throws IOException {
6055
System.exit(-1);
6156
}
6257
pomExists();
63-
Path currentPath = Paths.get("").toAbsolutePath();
64-
Path currentDirectory = currentPath.getFileName();
65-
String thinWARName = currentDirectory + ".war";
6658

67-
Path thinWARPath = Paths.get("target", thinWARName);
59+
Path thinWARPath = Paths.get("target");
6860

6961
Set<Path> deploymentDirs = Configurator.getConfiguredFolders(convert(args));
7062
validateDeploymentDirectories(deploymentDirs);
7163

72-
List<Path> deploymentTargets = addWarName(deploymentDirs, thinWARName);
7364
Path sourceCodeDir = Paths.get("./src/main/");
74-
System.out.printf("WAD is watching %s, deploying %s to %s \n", sourceCodeDir, thinWARPath, deploymentTargets);
75-
WADFlow wadFlow = new WADFlow(sourceCodeDir, thinWARPath, deploymentTargets);
65+
System.out.printf("WAD is watching %s, deploying from dir '%s' to %s \n", sourceCodeDir, thinWARPath, deploymentDirs);
66+
WADFlow wadFlow = new WADFlow(sourceCodeDir, thinWARPath, deploymentDirs);
7667
}
7768

7869
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.airhacks.wad.control;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.util.Optional;
8+
import org.junit.Test;
9+
10+
public class CopierTest {
11+
12+
@Test
13+
public void testFindWarInPath() {
14+
15+
Path path = Paths.get("src/test/resources/result");
16+
Optional<Path> warInPath = new Copier(null, null).findWarInPath(path);
17+
assertTrue(warInPath.isPresent());
18+
assertTrue(warInPath.get().getFileName().endsWith("myApp_1.0.0-SNAPSHOT.war"));
19+
}
20+
}

src/test/java/wad/AppTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package wad;
22

3-
import java.nio.file.Path;
4-
import java.nio.file.Paths;
53
import static org.hamcrest.CoreMatchers.is;
64
import static org.junit.Assert.assertThat;
5+
6+
import java.io.File;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
79
import org.junit.Test;
810

911
/**
@@ -21,14 +23,13 @@ public void currentFolderFolderName() {
2123

2224
@Test
2325
public void addTrailingSlash() {
24-
String expected = "/";
26+
String expected = File.separator;
2527

2628
String actual = App.addTrailingSlash("").toString();
2729
assertThat(actual, is(expected));
2830

29-
actual = App.addTrailingSlash("/").toString();
31+
actual = App.addTrailingSlash(File.separator).toString();
3032
assertThat(actual, is(expected));
3133
}
3234

33-
3435
}

src/test/resources/result/myApp_1.0.0-SNAPSHOT.war

Whitespace-only changes.

0 commit comments

Comments
 (0)