Skip to content

Commit 8e6569f

Browse files
committed
BAEL-6007: multi-module maven project with docker
1 parent 72a47b4 commit 8e6569f

File tree

14 files changed

+290
-1
lines changed

14 files changed

+290
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM maven:3.8.5-openjdk-17 AS MAVEN_TOOL_CHAIN
2+
3+
COPY pom.xml /tmp/
4+
COPY api /tmp/api/
5+
COPY domain /tmp/domain/
6+
7+
WORKDIR /tmp/
8+
RUN mvn clean install -X
9+
10+
FROM openjdk:17
11+
12+
COPY --from=MAVEN_TOOL_CHAIN /tmp/api/target/docker-multi-module-maven-api-0.0.1-SNAPSHOT.jar /app.jar
13+
14+
ENTRYPOINT ["java", "-jar", "/app.jar"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
### Relevant Articles:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GET http://localhost:8080/api/countries
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>docker-multi-module-maven-api</artifactId>
7+
8+
<parent>
9+
<groupId>com.baeldung</groupId>
10+
<artifactId>docker-multi-module-maven-parent</artifactId>
11+
<version>0.0.1-SNAPSHOT</version>
12+
</parent>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>com.baeldung</groupId>
21+
<artifactId>docker-multi-module-maven-domain</artifactId>
22+
<version>0.0.1-SNAPSHOT</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-test</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.api;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.domain.EntityScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8+
9+
@SpringBootApplication
10+
public class Application {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(Application.class, args);
14+
}
15+
16+
@Configuration
17+
@EntityScan(basePackages = "com.baeldung.domain")
18+
@EnableJpaRepositories(basePackages = "com.baeldung.domain")
19+
static class Config {
20+
}
21+
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.api;
2+
3+
import com.baeldung.domain.Country;
4+
import com.baeldung.domain.CountryRepository;
5+
import org.springframework.boot.CommandLineRunner;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
class StaticDataLoader implements CommandLineRunner {
10+
private final CountryRepository countries;
11+
12+
public StaticDataLoader(CountryRepository countries) {
13+
this.countries = countries;
14+
}
15+
16+
@Override
17+
public void run(String... args) {
18+
countries.save(new Country(1L, "US", "United States", ":us:"));
19+
countries.save(new Country(2L, "CA", "Canada", ":canada:"));
20+
countries.save(new Country(3L, "GB", "United Kingdom", ":uk:"));
21+
countries.save(new Country(4L, "AU", "Romania", ":romania:"));
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.api.controller;
2+
3+
import com.baeldung.domain.Country;
4+
import com.baeldung.domain.CountryRepository;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import java.util.List;
9+
10+
import static java.util.stream.Collectors.toList;
11+
import static java.util.stream.StreamSupport.stream;
12+
13+
@RestController
14+
public class CountriesController {
15+
private final CountryRepository countries;
16+
17+
public CountriesController(CountryRepository countries) {
18+
this.countries = countries;
19+
}
20+
21+
@GetMapping("api/countries")
22+
public List<CountryDto> index() {
23+
Iterable<Country> all = countries.findAll();
24+
return stream(all.spliterator(), false)
25+
.map(it -> new CountryDto(it.getId(), it.getName(), it.getIso(), null))
26+
.collect(toList());
27+
}
28+
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.api.controller;
2+
3+
public class CountryDto {
4+
private final Long id;
5+
private final String name;
6+
private final String code;
7+
private final String emoji;
8+
9+
public CountryDto(Long id, String name, String code, String emoji) {
10+
this.id = id;
11+
this.name = name;
12+
this.code = code;
13+
this.emoji = emoji;
14+
}
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public String getCode() {
25+
return code;
26+
}
27+
28+
public String getEmoji() {
29+
return emoji;
30+
}
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring:
2+
jpa:
3+
hibernate:
4+
ddl-auto: create-drop
5+
6+
level:
7+
org.springframework.jdbc.core: DEBUG
8+
org.hibernate.SQL: DEBUG
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>docker-multi-module-maven-domain</artifactId>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>docker-multi-module-maven-parent</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-data-jpa</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.h2database</groupId>
22+
<artifactId>h2</artifactId>
23+
<scope>runtime</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-test</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<plugins>
34+
<!-- Override Spring Boot plugin in this module to prevent it from running -->
35+
<plugin>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-maven-plugin</artifactId>
38+
<executions>
39+
<execution>
40+
<id>repackage</id>
41+
<phase>none</phase>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>

0 commit comments

Comments
 (0)