Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package com.baeldung.algorithms.dfs;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class BinaryTree {
|
||||
@@ -124,69 +122,43 @@ public class BinaryTree {
|
||||
}
|
||||
}
|
||||
|
||||
public void traverseLevelOrder() {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Queue<Node> nodes = new LinkedList<>();
|
||||
nodes.add(root);
|
||||
|
||||
while (!nodes.isEmpty()) {
|
||||
|
||||
Node node = nodes.remove();
|
||||
|
||||
System.out.print(" " + node.value);
|
||||
|
||||
if (node.left != null) {
|
||||
nodes.add(node.left);
|
||||
}
|
||||
|
||||
if (node.left != null) {
|
||||
nodes.add(node.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void traverseInOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Stack<Node> stack = new Stack<>();
|
||||
Node current = root;
|
||||
stack.push(root);
|
||||
while(! stack.isEmpty()) {
|
||||
while(current.left != null) {
|
||||
current = current.left;
|
||||
stack.push(current);
|
||||
}
|
||||
current = stack.pop();
|
||||
visit(current.value);
|
||||
if(current.right != null) {
|
||||
current = current.right;
|
||||
|
||||
while (current != null || !stack.isEmpty()) {
|
||||
while (current != null) {
|
||||
stack.push(current);
|
||||
current = current.left;
|
||||
}
|
||||
|
||||
Node top = stack.pop();
|
||||
visit(top.value);
|
||||
current = top.right;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void traversePreOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Node current = root;
|
||||
Stack<Node> stack = new Stack<>();
|
||||
Node current;
|
||||
stack.push(root);
|
||||
while(! stack.isEmpty()) {
|
||||
current = stack.pop();
|
||||
visit(current.value);
|
||||
|
||||
|
||||
if(current.right != null)
|
||||
stack.push(current.right);
|
||||
|
||||
|
||||
if(current.left != null)
|
||||
stack.push(current.left);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void traversePostOrderWithoutRecursion() {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Stack<Node> stack = new Stack<>();
|
||||
Node prev = root;
|
||||
Node current = root;
|
||||
Node current;
|
||||
stack.push(root);
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
@@ -206,14 +178,14 @@ public class BinaryTree {
|
||||
stack.push(current.left);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void visit(int value) {
|
||||
System.out.print(" " + value);
|
||||
}
|
||||
}
|
||||
|
||||
class Node {
|
||||
|
||||
private void visit(int value) {
|
||||
System.out.print(" " + value);
|
||||
}
|
||||
|
||||
static class Node {
|
||||
int value;
|
||||
Node left;
|
||||
Node right;
|
||||
|
||||
+3
-11
@@ -1,11 +1,11 @@
|
||||
package com.baeldung.algorithms.dfs;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BinaryTreeUnitTest {
|
||||
|
||||
@Test
|
||||
@@ -13,7 +13,7 @@ public class BinaryTreeUnitTest {
|
||||
|
||||
BinaryTree bt = createBinaryTree();
|
||||
|
||||
assertTrue(!bt.isEmpty());
|
||||
assertFalse(bt.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,14 +111,6 @@ public class BinaryTreeUnitTest {
|
||||
bt.traversePostOrderWithoutRecursion();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenABinaryTree_WhenTraversingLevelOrder_ThenPrintValues() {
|
||||
|
||||
BinaryTree bt = createBinaryTree();
|
||||
|
||||
bt.traverseLevelOrder();
|
||||
}
|
||||
|
||||
private BinaryTree createBinaryTree() {
|
||||
BinaryTree bt = new BinaryTree();
|
||||
|
||||
|
||||
@@ -8,4 +8,5 @@ This module contains articles about Java 8 core features
|
||||
- [Java 8 Stream skip() vs limit()](https://www.baeldung.com/java-stream-skip-vs-limit)
|
||||
- [Guide to Java BiFunction Interface](https://www.baeldung.com/java-bifunction-interface)
|
||||
- [Interface With Default Methods vs Abstract Class](https://www.baeldung.com/java-interface-default-method-vs-abstract-class)
|
||||
- [Convert Between Byte Array and UUID in Java](https://www.baeldung.com/java-byte-array-to-uuid)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-8)
|
||||
|
||||
+21
-14
@@ -1,17 +1,24 @@
|
||||
package com.baeldung.game;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
import java.util.*;
|
||||
|
||||
class RockPaperScissorsGame {
|
||||
|
||||
private static Map<Integer, String> movesMap = new HashMap<Integer, String>() {{
|
||||
put(0, "rock");
|
||||
put(1, "paper");
|
||||
put(2, "scissors");
|
||||
}};
|
||||
enum Move {
|
||||
ROCK("rock"),
|
||||
PAPER("paper"),
|
||||
SCISSORS("scissors");
|
||||
|
||||
private String value;
|
||||
|
||||
Move(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
@@ -31,7 +38,7 @@ class RockPaperScissorsGame {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!movesMap.containsValue(playerMove)) {
|
||||
if (Arrays.stream(Move.values()).noneMatch(x -> x.getValue().equals(playerMove))) {
|
||||
System.out.println("Your move isn't valid!");
|
||||
continue;
|
||||
}
|
||||
@@ -51,15 +58,15 @@ class RockPaperScissorsGame {
|
||||
}
|
||||
|
||||
private static boolean isPlayerWin(String playerMove, String computerMove) {
|
||||
return playerMove.equals("rock") && computerMove.equals("scissors")
|
||||
|| (playerMove.equals("scissors") && computerMove.equals("paper"))
|
||||
|| (playerMove.equals("paper") && computerMove.equals("rock"));
|
||||
return playerMove.equals(Move.ROCK.value) && computerMove.equals(Move.SCISSORS.value)
|
||||
|| (playerMove.equals(Move.SCISSORS.value) && computerMove.equals(Move.PAPER.value))
|
||||
|| (playerMove.equals(Move.PAPER.value) && computerMove.equals(Move.ROCK.value));
|
||||
}
|
||||
|
||||
private static String getComputerMove() {
|
||||
Random random = new Random();
|
||||
int randomNumber = random.nextInt(3);
|
||||
String computerMove = movesMap.get(randomNumber);
|
||||
String computerMove = Move.values()[randomNumber].getValue();
|
||||
System.out.println("Computer move: " + computerMove);
|
||||
return computerMove;
|
||||
}
|
||||
|
||||
@@ -7,3 +7,4 @@
|
||||
- [ArrayList vs. LinkedList vs. HashMap in Java](https://www.baeldung.com/java-arraylist-vs-linkedlist-vs-hashmap)
|
||||
- [Java Deque vs. Stack](https://www.baeldung.com/java-deque-vs-stack)
|
||||
- [Collection.toArray(new T[0]) or .toArray(new T[size])](https://www.baeldung.com/java-collection-toarray-methods)
|
||||
- [Create an Empty Map in Java](https://www.baeldung.com/java-create-empty-map)
|
||||
|
||||
@@ -15,6 +15,16 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
@@ -37,12 +47,23 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.entries;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class SimpleCustomKeyValue<K, V> implements Map.Entry<K, V> {
|
||||
|
||||
private final K key;
|
||||
private V value;
|
||||
|
||||
public SimpleCustomKeyValue(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V setValue(V value) {
|
||||
return this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
SimpleCustomKeyValue<?, ?> that = (SimpleCustomKeyValue<?, ?>) o;
|
||||
return Objects.equals(key, that.key) && Objects.equals(value, that.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", SimpleCustomKeyValue.class.getSimpleName() + "[", "]").add("key=" + key).add("value=" + value).toString();
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
package com.baeldung.entries;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.apache.commons.collections4.KeyValue;
|
||||
import org.apache.commons.collections4.keyvalue.DefaultMapEntry;
|
||||
import org.apache.commons.collections4.keyvalue.UnmodifiableMapEntry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.tuple;
|
||||
|
||||
public class EntriesExampleUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenEntries_whenVerifying_thenShouldContainKeyValues() {
|
||||
AbstractMap.SimpleEntry<String, String> firstEntry = new AbstractMap.SimpleEntry<>("key1", "value1");
|
||||
AbstractMap.SimpleEntry<String, String> secondEntry = new AbstractMap.SimpleEntry<>("key2", "value2");
|
||||
AbstractMap.SimpleEntry<String, String> thirdEntry = new AbstractMap.SimpleEntry<>(firstEntry);
|
||||
thirdEntry.setValue("a different value");
|
||||
|
||||
assertThat(Stream.of(firstEntry, secondEntry, thirdEntry))
|
||||
.extracting("key", "value")
|
||||
.containsExactly(
|
||||
tuple("key1", "value1"),
|
||||
tuple("key2", "value2"),
|
||||
tuple("key1", "a different value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImmutableEntries_whenVerifying_thenShouldContainKeyValues() {
|
||||
AbstractMap.SimpleImmutableEntry<String, String> firstEntry = new AbstractMap.SimpleImmutableEntry<>("key1", "value1");
|
||||
AbstractMap.SimpleImmutableEntry<String, String> secondEntry = new AbstractMap.SimpleImmutableEntry<>("key2", "value2");
|
||||
AbstractMap.SimpleImmutableEntry<String, String> thirdEntry = new AbstractMap.SimpleImmutableEntry<>(firstEntry);
|
||||
|
||||
assertThat(Stream.of(firstEntry, secondEntry, thirdEntry))
|
||||
.extracting("key", "value")
|
||||
.containsExactly(
|
||||
tuple("key1", "value1"),
|
||||
tuple("key2", "value2"),
|
||||
tuple("key1", "value1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImmutableEntryUsingJava9_whenVerifying_thenShouldContainKeyValues() {
|
||||
Map.Entry<String, String> entry = Map.entry("key", "value");
|
||||
|
||||
assertThat(entry.getKey())
|
||||
.isEqualTo("key");
|
||||
assertThat(entry.getValue())
|
||||
.isEqualTo("value");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenEntriesWithApacheCommons_whenVerifying_thenShouldContainKeyValues() {
|
||||
Map.Entry<String, String> firstEntry = new DefaultMapEntry<>("key1", "value1");
|
||||
KeyValue<String, String> secondEntry = new DefaultMapEntry<>("key2", "value2");
|
||||
|
||||
KeyValue<String, String> thirdEntry = new DefaultMapEntry<>(firstEntry);
|
||||
KeyValue<String, String> fourthEntry = new DefaultMapEntry<>(secondEntry);
|
||||
|
||||
firstEntry.setValue("a different value");
|
||||
|
||||
assertThat(firstEntry)
|
||||
.extracting("key", "value")
|
||||
.containsExactly("key1", "a different value");
|
||||
|
||||
assertThat(Stream.of(secondEntry, thirdEntry, fourthEntry))
|
||||
.extracting("key", "value")
|
||||
.containsExactly(
|
||||
tuple("key2", "value2"),
|
||||
tuple("key1", "value1"),
|
||||
tuple("key2", "value2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImmutableEntriesWithApacheCommons_whenVerifying_thenShouldContainKeyValues() {
|
||||
Map.Entry<String, String> firstEntry = new UnmodifiableMapEntry<>("key1", "value1");
|
||||
KeyValue<String, String> secondEntry = new UnmodifiableMapEntry<>("key2", "value2");
|
||||
|
||||
KeyValue<String, String> thirdEntry = new UnmodifiableMapEntry<>(firstEntry);
|
||||
KeyValue<String, String> fourthEntry = new UnmodifiableMapEntry<>(secondEntry);
|
||||
|
||||
assertThat(firstEntry)
|
||||
.extracting("key", "value")
|
||||
.containsExactly("key1", "value1");
|
||||
|
||||
assertThat(Stream.of(secondEntry, thirdEntry, fourthEntry))
|
||||
.extracting("key", "value")
|
||||
.containsExactly(
|
||||
tuple("key2", "value2"),
|
||||
tuple("key1", "value1"),
|
||||
tuple("key2", "value2"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenImmutableEntriesWithGuava_whenVerifying_thenShouldContainKeyValues() {
|
||||
Map.Entry<String, String> firstEntry = Maps.immutableEntry("key1", "value1");
|
||||
Map.Entry<String, String> secondEntry = Maps.immutableEntry("key2", "value2");
|
||||
|
||||
assertThat(Stream.of(firstEntry, secondEntry))
|
||||
.extracting("key", "value")
|
||||
.containsExactly(
|
||||
tuple("key1", "value1"),
|
||||
tuple("key2", "value2"));
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.entries;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.tuple;
|
||||
|
||||
class SimpleCustomKeyValueUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void givenModifiableEntries_whenVerifying_thenShouldContainKeyValues() {
|
||||
Map.Entry<String, String> firstEntry = new SimpleCustomKeyValue<>("key1", "value1");
|
||||
|
||||
Map.Entry<String, String> secondEntry = new SimpleCustomKeyValue<>("key2", "value2");
|
||||
secondEntry.setValue("different value");
|
||||
|
||||
Map<String, String> map = Map.ofEntries(firstEntry, secondEntry);
|
||||
|
||||
assertThat(map)
|
||||
.isEqualTo(ImmutableMap.<String,String>builder()
|
||||
.put("key1", "value1")
|
||||
.put("key2", "different value")
|
||||
.build());
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.exception.variablemightnothavebeeninitialized;
|
||||
|
||||
public class VariableMightNotHaveBeenInitialized {
|
||||
|
||||
private static int instanceVariableCount;
|
||||
|
||||
/**
|
||||
* Method would not compile if lines 14 and 18 are uncommented.
|
||||
*/
|
||||
public static void countEven() {
|
||||
//uninstantiated
|
||||
int count;
|
||||
int[] arr = new int[]{23, 56, 89, 12, 23};
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
if ((arr[i] % 2) == 0) {
|
||||
// count++;
|
||||
}
|
||||
|
||||
}
|
||||
// System.out.println("Total Even Numbers : " + count);
|
||||
}
|
||||
|
||||
public static int countEvenUsingInstanceVariable(int[] arr) {
|
||||
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
if ((arr[i] % 2) == 0) {
|
||||
instanceVariableCount++;
|
||||
}
|
||||
|
||||
}
|
||||
return instanceVariableCount;
|
||||
}
|
||||
|
||||
public static int countEvenUsingIfElse(int[] arr, int args) {
|
||||
int count;
|
||||
count = args > 0 ? args : 0;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
if ((arr[i] % 2) == 0) {
|
||||
count++;
|
||||
}
|
||||
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.exception.variablemightnothavebeeninitialized;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class VariableMightNotHaveBeenInitializedUnitTest {
|
||||
|
||||
@Test
|
||||
public void usingInstanceVariable_returnCount() {
|
||||
int[] arr = new int[]{1, 2, 3, 4, 5, 6};
|
||||
int value = VariableMightNotHaveBeenInitialized.countEvenUsingInstanceVariable(arr);
|
||||
|
||||
assertEquals(3, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usingArgumentsAndIfElse_returnCount() {
|
||||
int[] arr = new int[]{1, 2, 3, 4, 5, 6};
|
||||
int value = VariableMightNotHaveBeenInitialized.countEvenUsingIfElse(arr, 2);
|
||||
|
||||
assertEquals(5, value);
|
||||
}
|
||||
}
|
||||
+339
File diff suppressed because it is too large
Load Diff
@@ -10,4 +10,5 @@ This module contains articles about core Java Security
|
||||
- [HMAC in Java](https://www.baeldung.com/java-hmac)
|
||||
- [Generating a Secure AES Key in Java](https://www.baeldung.com/java-secure-aes-key)
|
||||
- [Computing an X509 Certificate’s Thumbprint in Java](https://www.baeldung.com/java-x509-certificate-thumbprint)
|
||||
- [Error: “trustAnchors parameter must be non-empty”](https://www.baeldung.com/java-trustanchors-parameter-must-be-non-empty)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-security-2)
|
||||
|
||||
@@ -38,7 +38,6 @@
|
||||
<module>core-java-collections-maps</module>
|
||||
<module>core-java-collections-maps-2</module>
|
||||
<module>core-java-collections-maps-3</module>
|
||||
<module>core-java-collections-maps-4</module>
|
||||
<module>core-java-concurrency-2</module>
|
||||
<module>core-java-concurrency-advanced</module>
|
||||
<module>core-java-concurrency-advanced-2</module>
|
||||
|
||||
@@ -6,3 +6,4 @@
|
||||
- [How To Configure Java Heap Size Inside a Docker Container](https://www.baeldung.com/ops/docker-jvm-heap-size)
|
||||
- [Dockerfile Strategies for Git](https://www.baeldung.com/ops/dockerfile-git-strategies)
|
||||
- [How to Get Docker-Compose to Always Use the Latest Image](https://www.baeldung.com/ops/docker-compose-latest-image)
|
||||
- [How to Include Files Outside of Docker’s Build Context](https://www.baeldung.com/ops/docker-include-files-outside-build-context)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
FROM maven:alpine as build
|
||||
ENV HOME=/usr/app
|
||||
RUN mkdir -p $HOME
|
||||
WORKDIR $HOME
|
||||
|
||||
ADD pom.xml $HOME
|
||||
ADD core/pom.xml $HOME/core/pom.xml
|
||||
ADD runner/pom.xml $HOME/runner/pom.xml
|
||||
|
||||
RUN mvn -pl core verify --fail-never
|
||||
ADD core $HOME/core
|
||||
RUN mvn -pl core install
|
||||
RUN mvn -pl runner verify --fail-never
|
||||
ADD runner $HOME/runner
|
||||
RUN mvn -pl core,runner package
|
||||
|
||||
FROM openjdk:8-jdk-alpine
|
||||
|
||||
COPY --from=build /usr/app/runner/target/runner-0.0.1-SNAPSHOT-jar-with-dependencies.jar /app/runner.jar
|
||||
|
||||
ENTRYPOINT java -jar /app/runner.jar
|
||||
@@ -0,0 +1,13 @@
|
||||
FROM maven:alpine as build
|
||||
ENV HOME=/usr/app
|
||||
RUN mkdir -p $HOME
|
||||
WORKDIR $HOME
|
||||
|
||||
ADD . $HOME
|
||||
RUN --mount=type=cache,target=/root/.m2 mvn -f $HOME/pom.xml clean package
|
||||
|
||||
FROM openjdk:8-jdk-alpine
|
||||
|
||||
COPY --from=build /usr/app/runner/target/runner-0.0.1-SNAPSHOT-jar-with-dependencies.jar /app/runner.jar
|
||||
|
||||
ENTRYPOINT java -jar /app/runner.jar
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core</artifactId>
|
||||
|
||||
<parent>
|
||||
<artifactId>multi-module-caching</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.maven_caching;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
|
||||
public class CoreClass {
|
||||
|
||||
public String method() {
|
||||
return "Hello from core module!!";
|
||||
}
|
||||
|
||||
public String dependencyMethod() {
|
||||
return Files.simplifyPath("/home/app/test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>multi-module-caching</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<description>Multi-module Maven caching example</description>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>31.0.1-jre</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>runner</module>
|
||||
<module>core</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>runner</artifactId>
|
||||
|
||||
<parent>
|
||||
<artifactId>multi-module-caching</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.baeldung.maven_caching.MavenCachingApplication</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>assemble-all</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.maven_caching;
|
||||
|
||||
public class MavenCachingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CoreClass cc = new CoreClass();
|
||||
System.out.println(cc.method());
|
||||
System.out.println(cc.dependencyMethod());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
FROM maven:alpine as build
|
||||
ENV HOME=/usr/app
|
||||
RUN mkdir -p $HOME
|
||||
WORKDIR $HOME
|
||||
ADD pom.xml $HOME
|
||||
RUN mvn verify --fail-never
|
||||
|
||||
ADD . $HOME
|
||||
RUN mvn package
|
||||
|
||||
FROM openjdk:8-jdk-alpine
|
||||
|
||||
COPY --from=build /usr/app/target/single-module-caching-1.0-SNAPSHOT-jar-with-dependencies.jar /app/runner.jar
|
||||
|
||||
ENTRYPOINT java -jar /app/runner.jar
|
||||
@@ -0,0 +1,13 @@
|
||||
FROM maven:alpine as build
|
||||
ENV HOME=/usr/app
|
||||
RUN mkdir -p $HOME
|
||||
WORKDIR $HOME
|
||||
ADD . $HOME
|
||||
|
||||
RUN --mount=type=cache,target=/root/.m2 mvn -f $HOME/pom.xml clean package
|
||||
|
||||
FROM openjdk:8-jdk-alpine
|
||||
|
||||
COPY --from=build /usr/app/target/single-module-caching-1.0-SNAPSHOT-jar-with-dependencies.jar /app/runner.jar
|
||||
|
||||
ENTRYPOINT java -jar /app/runner.jar
|
||||
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>single-module-caching</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>31.0.1-jre</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.baeldung.maven_caching.MavenCachingMain</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>assemble-all</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
</project>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.maven_caching;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
|
||||
public class MavenCachingMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello from maven_caching app!!!");
|
||||
System.out.println(Files.simplifyPath("/home/app/test"));
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,8 @@
|
||||
<module>docker-internal-dto</module>
|
||||
<module>docker-spring-boot</module>
|
||||
<module>docker-sample-app</module>
|
||||
<module>docker-caching/single-module-caching</module>
|
||||
<module>docker-caching/multi-module-caching</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Gradle: sourceCompatiblity vs targetCompatibility](https://www.baeldung.com/gradle-sourcecompatiblity-vs-targetcompatibility)
|
||||
@@ -7,3 +7,4 @@
|
||||
- [Update the Value Associated With a Key in a HashMap](https://www.baeldung.com/java-hashmap-update-value-by-key)
|
||||
- [Java Map – keySet() vs. entrySet() vs. values() Methods](https://www.baeldung.com/java-map-entries-methods)
|
||||
- [Java IdentityHashMap Class and Its Use Cases](https://www.baeldung.com/java-identityhashmap)
|
||||
- [How to Invert a Map in Java](https://www.baeldung.com/java-invert-map)
|
||||
|
||||
@@ -7,3 +7,4 @@ This module contains articles about the Java Native Interface (JNI).
|
||||
- [Guide to JNI (Java Native Interface)](https://www.baeldung.com/jni)
|
||||
- [Using JNA to Access Native Dynamic Libraries](https://www.baeldung.com/java-jna-dynamic-libraries)
|
||||
- [Check if a Java Program Is Running in 64-Bit or 32-Bit JVM](https://www.baeldung.com/java-detect-jvm-64-or-32-bit)
|
||||
- [How to use JNI’s RegisterNatives() method?](https://www.baeldung.com/jni-registernatives)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user