Skip to content

Commit e52f11b

Browse files
authored
OWLS-102537 Display exporter version (#202)
1 parent ad9d535 commit e52f11b

File tree

14 files changed

+707
-95
lines changed

14 files changed

+707
-95
lines changed

build-helper-mojo/pom.xml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
<dependencies>
2121
<dependency>
2222
<groupId>org.apache.maven</groupId>
23-
<artifactId>maven-plugin-api</artifactId>
24-
<version>3.8.6</version>
23+
<artifactId>maven-project</artifactId>
24+
<version>2.2.1</version>
2525
<scope>provided</scope>
2626
</dependency>
2727
<dependency>
2828
<groupId>org.apache.maven</groupId>
29-
<artifactId>maven-model</artifactId>
29+
<artifactId>maven-plugin-api</artifactId>
3030
<version>3.8.6</version>
3131
<scope>provided</scope>
3232
</dependency>
3333
<dependency>
3434
<groupId>org.apache.maven</groupId>
35-
<artifactId>maven-artifact</artifactId>
35+
<artifactId>maven-model</artifactId>
3636
<version>3.8.6</version>
3737
<scope>provided</scope>
3838
</dependency>
@@ -62,10 +62,19 @@
6262
<artifactId>simplestub</artifactId>
6363
<scope>test</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>com.google.guava</groupId>
67+
<artifactId>guava</artifactId>
68+
</dependency>
6569
</dependencies>
6670

6771
<build>
6872
<plugins>
73+
<plugin>
74+
<groupId>org.jacoco</groupId>
75+
<artifactId>jacoco-maven-plugin</artifactId>
76+
</plugin>
77+
6978
<plugin>
7079
<groupId>org.apache.maven.plugins</groupId>
7180
<artifactId>maven-clean-plugin</artifactId>
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
// Copyright (c) 2020, Oracle and/or its affiliates.
1+
// Copyright (c) 2020, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.wls.buildhelper;
55

66
import java.io.File;
7-
import java.io.IOException;
87
import java.nio.file.Path;
98

109
public interface CopyExecutor {
1110

1211
Path toPath(File file);
1312

14-
void copyFile(Path sourcePath, Path targetPath) throws IOException;
15-
1613
}

build-helper-mojo/src/main/java/com/oracle/wls/buildhelper/CopyExecutorImpl.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,13 @@
44
package com.oracle.wls.buildhelper;
55

66
import java.io.File;
7-
import java.io.IOException;
8-
import java.nio.file.Files;
97
import java.nio.file.Path;
108

11-
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
12-
139
public class CopyExecutorImpl implements CopyExecutor {
1410

1511
@Override
1612
public Path toPath(File file) {
1713
return file.toPath();
1814
}
1915

20-
@Override
21-
public void copyFile(Path sourcePath, Path targetPath) throws IOException {
22-
Files.createDirectories(targetPath.getParent());
23-
Files.copy(sourcePath, targetPath, REPLACE_EXISTING);
24-
}
2516
}

build-helper-mojo/src/main/java/com/oracle/wls/buildhelper/BuildHelperMojo.java renamed to build-helper-mojo/src/main/java/com/oracle/wls/buildhelper/FileCopyMojo.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
// Copyright (c) 2020, Oracle and/or its affiliates.
1+
// Copyright (c) 2020, 2022 Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.wls.buildhelper;
55

6-
import java.io.File;
7-
import java.io.IOException;
8-
import java.nio.file.Path;
9-
import java.util.Optional;
10-
116
import org.apache.maven.plugin.AbstractMojo;
127
import org.apache.maven.plugin.MojoExecutionException;
138
import org.apache.maven.plugins.annotations.LifecyclePhase;
149
import org.apache.maven.plugins.annotations.Mojo;
1510
import org.apache.maven.plugins.annotations.Parameter;
1611

12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.util.Optional;
17+
18+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
19+
1720
@Mojo(name = "copy", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
18-
public class BuildHelperMojo extends AbstractMojo {
21+
public class FileCopyMojo extends AbstractMojo {
1922
@Parameter(required = true) private String sourceFile;
2023
@Parameter(required = true) private File targetFile;
2124
@Parameter private File userDir;
@@ -28,7 +31,8 @@ public void execute() throws MojoExecutionException {
2831
Path target = toPath(targetFile);
2932
try {
3033
getLog().info("Copying " + source + " to " + target);
31-
executor.copyFile(source, target);
34+
Files.createDirectories(target.getParent());
35+
Files.copy(source, target, REPLACE_EXISTING);
3236
} catch (IOException e) {
3337
throw new MojoExecutionException("Unable to copy " + source + " to " + target, e);
3438
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2022, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.wls.buildhelper;
5+
6+
import org.apache.maven.plugin.MojoExecutionException;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Path;
11+
12+
/**
13+
* An interface for the system interaction functionality required to execute the GitTag mojo.
14+
*/
15+
interface GitTagExecutor {
16+
17+
/**
18+
* Issues a command and returns the response.
19+
* @param commandLine the command to execute
20+
*/
21+
String runCommand(String... commandLine) throws IOException, MojoExecutionException;
22+
23+
Path toPath(File file);
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.wls.buildhelper;
5+
6+
import org.apache.maven.plugin.MojoExecutionException;
7+
8+
import java.io.BufferedReader;
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.io.InputStreamReader;
12+
import java.nio.file.Path;
13+
import java.util.stream.Collectors;
14+
15+
public class GitTagExecutorImpl implements GitTagExecutor {
16+
@Override
17+
public String runCommand(String... commandLineStrings) throws IOException, MojoExecutionException {
18+
try {
19+
final Process vp = new ProcessBuilder(commandLineStrings).start();
20+
21+
vp.waitFor();
22+
23+
final BufferedReader br = new BufferedReader(new InputStreamReader(vp.getInputStream()));
24+
return br.lines().collect(Collectors.joining("\n"));
25+
} catch (InterruptedException e) {
26+
Thread.currentThread().interrupt();
27+
throw new MojoExecutionException("Thread interrupted while running command " + String.join(" ", commandLineStrings));
28+
}
29+
}
30+
31+
@Override
32+
public Path toPath(File file) {
33+
return file.toPath();
34+
}
35+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2022, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.wls.buildhelper;
5+
6+
import org.apache.maven.plugin.AbstractMojo;
7+
import org.apache.maven.plugin.MojoExecutionException;
8+
import org.apache.maven.plugins.annotations.LifecyclePhase;
9+
import org.apache.maven.plugins.annotations.Mojo;
10+
import org.apache.maven.plugins.annotations.Parameter;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.util.Arrays;
17+
import java.util.Collections;
18+
import java.util.List;
19+
20+
@Mojo(name = "gitVersion", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
21+
public class GitTagMojo extends AbstractMojo {
22+
23+
@Parameter(required = true, defaultValue = "${project.build.outputDirectory}/version.properties")
24+
private File outputFile;
25+
26+
private final GitTagExecutor executor;
27+
28+
GitTagMojo(GitTagExecutor executor) {
29+
this.executor = executor;
30+
}
31+
32+
@SuppressWarnings("unused")
33+
public GitTagMojo() {
34+
this(new GitTagExecutorImpl());
35+
}
36+
37+
@Override
38+
public void execute() throws MojoExecutionException {
39+
try {
40+
final String versionString = toVersionString(executor.runCommand("git", "describe", "--tag"));
41+
final Path path = executor.toPath(outputFile);
42+
Files.createDirectories(path.getParent());
43+
Files.write(path, createProperties(versionString));
44+
} catch (IOException e) {
45+
throw new MojoExecutionException("Error writing " + outputFile + ": " + e );
46+
}
47+
48+
}
49+
50+
private List<String> createProperties(String versionString) {
51+
return Collections.singletonList("version=" + versionString);
52+
}
53+
54+
// parses the result of 'git describe --tag' to describe the current code version/
55+
private String toVersionString(String gitResponse) {
56+
final String[] segments = gitResponse.split("-");
57+
if (segments.length == 1 || segments.length == 2)
58+
return gitResponse;
59+
else
60+
return formatVersionString(segments);
61+
}
62+
63+
private String formatVersionString(String[] segments) {
64+
final String commit = segments[segments.length - 1];
65+
final String numCommits = segments[segments.length - 2];
66+
final String versionParts = String.join("-", Arrays.copyOfRange(segments, 0, segments.length - 2));
67+
return formatVersionString(commit, numCommits, versionParts);
68+
}
69+
70+
private String formatVersionString(String commit, String numCommits, String versionParts) {
71+
return commit + " (" + numCommits + " commits since " + versionParts + ")";
72+
}
73+
}

0 commit comments

Comments
 (0)