Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit ffffef6

Browse files
authored
Merge pull request #65 from hivemq/develop
Version 1.3.0
2 parents fb2dad2 + 91ce5a2 commit ffffef6

File tree

51 files changed

+1624
-307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1624
-307
lines changed

build.gradle

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ plugins {
33
id 'com.github.hierynomus.license' version '0.15.0' apply false
44
}
55

6-
76
/* ******************** metadata ******************** */
87

98
allprojects {
10-
version '1.2.0'
9+
version '1.3.0'
1110
group 'com.hivemq'
1211
description 'Testcontainers for testing HiveMQ Extensions and Java MQTT Applications.'
1312

@@ -48,21 +47,23 @@ allprojects {
4847
}
4948

5049
ext {
51-
testContainersVersion = '1.14.3'
52-
extensionSDKVersion = '4.4.0'
50+
testContainersVersion = '1.15.0'
51+
extensionSDKVersion = '4.4.4'
5352
hmcVersion = '1.2.1'
54-
junit4Version = '4.13'
55-
junit5Version = '5.6.2'
56-
commonsIoVersion = '2.7'
53+
junit4Version = '4.13.1'
54+
junit5Version = '5.7.0'
55+
jetbrainsAnnotationsVersion = '20.1.0'
56+
commonsIoVersion = '2.8.0'
57+
commonsLangVersion = '3.11'
5758
javassistVersion = '3.27.0-GA'
5859
shrinkWrapVersion = '1.2.6'
5960
shrinkwrapResolverDepchainVersion = '3.1.4'
60-
zip4jVersion = '2.6.2'
61+
zip4jVersion = '2.6.4'
62+
guavaVersion = '30.1-jre'
6163

6264
// only test
63-
httpClientVersion = '4.5.12'
65+
httpClientVersion = '4.5.13'
6466
logbackVersion = '1.2.3'
65-
daggerVersion = '2.27'
6667
}
6768
}
6869

core/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ ext {
99

1010
dependencies {
1111
api group: 'org.testcontainers', name: 'testcontainers', version: testContainersVersion
12+
api group: 'org.jetbrains', name: 'annotations', version: jetbrainsAnnotationsVersion
13+
implementation group: 'org.apache.commons', name: 'commons-lang3', version: commonsLangVersion
14+
implementation group: 'com.google.guava', name: 'guava', version: guavaVersion
1215
implementation group: 'commons-io', name: 'commons-io', version: commonsIoVersion
1316
implementation group: 'org.javassist', name: 'javassist', version: javassistVersion
1417
implementation group: 'org.jboss.shrinkwrap', name: 'shrinkwrap-api', version: shrinkWrapVersion
@@ -30,6 +33,4 @@ dependencies {
3033

3134
testImplementation group: 'com.hivemq', name: 'hivemq-extension-sdk', version: extensionSDKVersion
3235
testImplementation group: 'com.hivemq', name: 'hivemq-mqtt-client', version: hmcVersion
33-
testImplementation group: 'com.google.dagger', name: 'dagger', version: daggerVersion
34-
testAnnotationProcessor group: 'com.google.dagger', name: 'dagger-compiler', version: daggerVersion
3536
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* Copyright 2020 HiveMQ and the HiveMQ Community
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hivemq.testcontainer.core;
17+
18+
import net.lingala.zip4j.ZipFile;
19+
import org.apache.commons.lang3.SystemUtils;
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import java.io.BufferedReader;
23+
import java.io.File;
24+
import java.io.InputStreamReader;
25+
import java.nio.file.Files;
26+
import java.util.AbstractMap;
27+
import java.util.Map;
28+
import java.util.function.Supplier;
29+
import java.util.regex.Matcher;
30+
import java.util.regex.Pattern;
31+
import java.util.stream.Collectors;
32+
33+
/**
34+
* This class automates the process of packaging a HiveMQ extension from a gradle project.
35+
* It uses ./gradlew on MacOS and Linux and uses gradlew.bat on Windows to execute the gradle task.
36+
*
37+
* @author Yannick Weber
38+
* @since 1.3.0
39+
*/
40+
public class GradleHiveMQExtensionSupplier implements Supplier<File> {
41+
42+
private static final @NotNull Pattern ROOT_PROJECT_PATTERN = Pattern.compile(".*'(.*)'");
43+
private static final @NotNull String PROPERTY_REGEX = "(.*): (.*)";
44+
45+
private static final @NotNull String BUILD_STARTED =
46+
"=================================================================\n" +
47+
"=== Embedded Gradle build started: %s ===\n" +
48+
"=================================================================\n";
49+
50+
private static final @NotNull String BUILD_STOPPED =
51+
"=================================================================\n" +
52+
"=== Embedded Gradle build stopped: %s ===\n" +
53+
"=================================================================";
54+
55+
private static final @NotNull String TASK = "hivemqExtensionZip";
56+
57+
private final @NotNull File gradleProjectDirectory;
58+
private boolean quiet = false;
59+
60+
/**
61+
* This {@link Supplier} can be used if the current gradle project is the HiveMQ Extension to supply.
62+
* It uses the build.gradle file and the gradle wrapper of the current gradle project.
63+
*
64+
* @return a {@link GradleHiveMQExtensionSupplier} for the current gradle project
65+
* @since 1.3.0
66+
*/
67+
public static @NotNull GradleHiveMQExtensionSupplier direct() {
68+
69+
return new GradleHiveMQExtensionSupplier(new File(""));
70+
}
71+
72+
/**
73+
* Creates a Gradle HiveMQ extension {@link Supplier}.
74+
* It uses the gradle wrapper of the gradle project associated with the given It uses the build.gradle file.
75+
*
76+
* @param gradleProjectDirectory the gradle project directory of the HiveMQ extension to supply.
77+
* @since 1.3.0
78+
*/
79+
public GradleHiveMQExtensionSupplier(final @NotNull File gradleProjectDirectory) {
80+
81+
if (!gradleProjectDirectory.exists()) {
82+
throw new IllegalStateException(gradleProjectDirectory + " does not exist.");
83+
}
84+
if (!gradleProjectDirectory.canRead()) {
85+
throw new IllegalStateException(gradleProjectDirectory + " is not readable.");
86+
}
87+
this.gradleProjectDirectory = gradleProjectDirectory;
88+
}
89+
90+
/**
91+
* Packages the HiveMQ extension, copies it to a temporary directory and returns the directory as a {@link File}.
92+
*
93+
* @return the {@link File} of the packaged HiveMQ extension
94+
* @since 1.3.0
95+
*/
96+
@Override
97+
public @NotNull File get() {
98+
System.out.printf((BUILD_STARTED) + "%n", gradleProjectDirectory);
99+
100+
try {
101+
final ProcessBuilder extensionZipProcessBuilder = new ProcessBuilder();
102+
extensionZipProcessBuilder.directory(gradleProjectDirectory);
103+
extensionZipProcessBuilder.command(getCommandForOs(gradleProjectDirectory), TASK);
104+
extensionZipProcessBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
105+
106+
if (!quiet) {
107+
extensionZipProcessBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
108+
}
109+
final Process extensionZipProcess = extensionZipProcessBuilder.start();
110+
final int extensionZipExitCode = extensionZipProcess.waitFor();
111+
if (extensionZipExitCode != 0) {
112+
throw new IllegalStateException("Gradle build exited with code " + extensionZipExitCode);
113+
}
114+
115+
final ProcessBuilder propertiesProcessBuilder = new ProcessBuilder();
116+
propertiesProcessBuilder.directory(gradleProjectDirectory);
117+
propertiesProcessBuilder.command(getCommandForOs(gradleProjectDirectory), "properties", "-q");
118+
propertiesProcessBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
119+
120+
final Process propertiesProcess = propertiesProcessBuilder.start();
121+
final int propertiesExitCode = propertiesProcess.waitFor();
122+
if (propertiesExitCode != 0) {
123+
throw new IllegalStateException("Gradle build exited with code " + propertiesExitCode);
124+
}
125+
126+
final BufferedReader br = new BufferedReader(new InputStreamReader(propertiesProcess.getInputStream()));
127+
128+
final Map<String, String> gradleProperties = br.lines()
129+
.filter(s -> s.matches(PROPERTY_REGEX))
130+
.map(s -> {
131+
final String[] splits = s.split(": ");
132+
return new AbstractMap.SimpleEntry<>(splits[0], splits[1]);
133+
})
134+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
135+
136+
137+
System.out.printf((BUILD_STOPPED) + "%n", this.gradleProjectDirectory);
138+
139+
final String projectVersion = gradleProperties.get("version");
140+
final String rootProject = gradleProperties.get("rootProject");
141+
final Matcher matcher = ROOT_PROJECT_PATTERN.matcher(rootProject);
142+
final boolean b = matcher.find();
143+
final String projectName = matcher.group(1);
144+
145+
final ZipFile zipFile = new ZipFile(
146+
new File(gradleProjectDirectory,
147+
"build/hivemq-extension/" + projectName + "-" + projectVersion + ".zip"));
148+
final File tempDir = Files.createTempDirectory("").toFile();
149+
150+
zipFile.extractAll(tempDir.getAbsolutePath());
151+
return new File(tempDir, projectName);
152+
153+
} catch (final Exception e) {
154+
throw new RuntimeException("Exception while building the HiveMQ extension with gradle.", e);
155+
}
156+
}
157+
158+
private @NotNull String getCommandForOs(final @NotNull File gradleProjectFile) {
159+
if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX || SystemUtils.IS_OS_LINUX) {
160+
final String gradleWrapper = gradleProjectFile + "/gradlew";
161+
final File gradleWrapperBashFile = new File(gradleWrapper);
162+
if (gradleWrapperBashFile.exists()) {
163+
if (!gradleWrapperBashFile.canExecute()) {
164+
throw new IllegalStateException("Gradle Wrapper " + gradleWrapperBashFile.getAbsolutePath() + " can not be executed.");
165+
}
166+
return gradleWrapperBashFile.getAbsolutePath();
167+
}
168+
} else if (SystemUtils.IS_OS_WINDOWS) {
169+
final String gradleWrapperBat = gradleProjectFile + "/gradlew.bat";
170+
final File gradleWrapperBatFile = new File(gradleWrapperBat);
171+
if (gradleWrapperBatFile.exists()) {
172+
if (!gradleWrapperBatFile.canExecute()) {
173+
throw new IllegalStateException("Gradle Wrapper " + gradleWrapperBatFile.getAbsolutePath() + " can not be executed.");
174+
}
175+
return gradleWrapperBatFile.getAbsolutePath();
176+
}
177+
}
178+
throw new IllegalStateException("Unkown OS Version");
179+
180+
}
181+
182+
/**
183+
* Suppress stdout of the gradle build.
184+
*
185+
* @return self
186+
* @since 1.3.0
187+
*/
188+
@NotNull
189+
public GradleHiveMQExtensionSupplier quiet() {
190+
this.quiet = true;
191+
return this;
192+
}
193+
}

core/src/main/java/com/hivemq/testcontainer/core/HiveMQExtension.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
package com.hivemq.testcontainer.core;
1717

18+
import com.google.common.collect.ImmutableList;
1819
import com.hivemq.extension.sdk.api.ExtensionMain;
19-
import com.hivemq.extension.sdk.api.annotations.NotNull;
20-
import com.hivemq.extension.sdk.api.annotations.Nullable;
21-
import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
20+
import org.jetbrains.annotations.NotNull;
21+
import org.jetbrains.annotations.Nullable;
2222

2323
/**
2424
* @author Yannick Weber

0 commit comments

Comments
 (0)