Skip to content

Commit e61ee98

Browse files
authored
Merge pull request #17360 from etrandafir93/BAEL-6007-multi_module_maven_docker
BAEL-6007: multi module maven docker
2 parents 82f90e8 + e55db05 commit e61ee98

File tree

14 files changed

+312
-1
lines changed

14 files changed

+312
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM maven:3.8.5-openjdk-17 AS DEPENDENCIES
2+
3+
WORKDIR /opt/app
4+
COPY api/pom.xml api/pom.xml
5+
COPY domain/pom.xml domain/pom.xml
6+
COPY pom.xml .
7+
8+
RUN mvn -B -e org.apache.maven.plugins:maven-dependency-plugin:3.1.2:go-offline -DexcludeArtifactIds=domain
9+
10+
FROM maven:3.8.5-openjdk-17 AS BUILDER
11+
12+
WORKDIR /opt/app
13+
COPY --from=DEPENDENCIES /root/.m2 /root/.m2
14+
COPY --from=DEPENDENCIES /opt/app/ /opt/app
15+
COPY api/src /opt/app/api/src
16+
COPY domain/src /opt/app/domain/src
17+
18+
RUN mvn -B -e clean install -DskipTests
19+
20+
FROM openjdk:17-slim
21+
22+
WORKDIR /opt/app
23+
COPY --from=BUILDER /opt/app/api/target/*.jar /app.jar
24+
EXPOSE 8080
25+
26+
ENTRYPOINT ["java", "-jar", "/app.jar"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
### Relevant Articles:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GET http://localhost:8080/api/countries
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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>api</artifactId>
7+
8+
<parent>
9+
<groupId>com.baeldung.docker-multi-module-maven</groupId>
10+
<artifactId>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.docker-multi-module-maven</groupId>
21+
<artifactId>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+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-maven-plugin</artifactId>
37+
</plugin>
38+
<plugin>
39+
<groupId>com.google.cloud.tools</groupId>
40+
<artifactId>jib-maven-plugin</artifactId>
41+
<version>3.4.0</version>
42+
<configuration>
43+
<from>
44+
<image>openjdk:17-slim</image>
45+
</from>
46+
<to>
47+
<image>baeldung-demo:from-jib</image>
48+
</to>
49+
<container>
50+
<mainClass>com.baeldung.api.Application</mainClass>
51+
<ports>
52+
<port>8080</port>
53+
</ports>
54+
</container>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.api;
2+
3+
import com.baeldung.domain.Country;
4+
import com.baeldung.domain.CountryRepository;
5+
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
class StaticDataLoader implements CommandLineRunner {
11+
12+
private final CountryRepository countries;
13+
14+
public StaticDataLoader(CountryRepository countries) {
15+
this.countries = countries;
16+
}
17+
18+
@Override
19+
public void run(String... args) {
20+
countries.save(new Country(1L, "US", "United States", ":us:"));
21+
countries.save(new Country(2L, "CA", "Canada", ":canada:"));
22+
countries.save(new Country(3L, "GB", "United Kingdom", ":uk:"));
23+
countries.save(new Country(4L, "AU", "Romania", ":romania:"));
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.api.controller;
2+
3+
import com.baeldung.domain.Country;
4+
import com.baeldung.domain.CountryRepository;
5+
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import java.util.List;
10+
11+
import static java.util.stream.Collectors.toList;
12+
import static java.util.stream.StreamSupport.stream;
13+
14+
@RestController
15+
public class CountriesController {
16+
17+
private final CountryRepository countries;
18+
19+
public CountriesController(CountryRepository countries) {
20+
this.countries = countries;
21+
}
22+
23+
@GetMapping("api/countries")
24+
public List<CountryDto> index() {
25+
26+
Iterable<Country> all = countries.findAll();
27+
return stream(all.spliterator(), false).map(it -> new CountryDto(it.getId(), it.getName(), it.getIso(), it.getEmoji()))
28+
.collect(toList());
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.api.controller;
2+
3+
public class CountryDto {
4+
5+
private final Long id;
6+
private final String name;
7+
private final String code;
8+
private final String emoji;
9+
10+
public CountryDto(Long id, String name, String code, String emoji) {
11+
this.id = id;
12+
this.name = name;
13+
this.code = code;
14+
this.emoji = emoji;
15+
}
16+
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public String getCode() {
26+
return code;
27+
}
28+
29+
public String getEmoji() {
30+
return emoji;
31+
}
32+
}
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
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>domain</artifactId>
7+
<packaging>jar</packaging>
8+
9+
<parent>
10+
<groupId>com.baeldung.docker-multi-module-maven</groupId>
11+
<artifactId>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+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.domain;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Id;
5+
6+
@Entity
7+
public class Country {
8+
9+
@Id
10+
private Long id;
11+
private String iso;
12+
private String name;
13+
private String emoji;
14+
15+
public Country(Long id, String iso, String name, String emoji) {
16+
this.id = id;
17+
this.iso = iso;
18+
this.name = name;
19+
this.emoji = emoji;
20+
}
21+
22+
public Country() {
23+
}
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public String getIso() {
30+
return iso;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public String getEmoji() {
38+
return emoji;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.domain;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface CountryRepository extends CrudRepository<Country, Long> {
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
7+
<groupId>com.baeldung.docker-multi-module-maven</groupId>
8+
<artifactId>parent</artifactId>
9+
<packaging>pom</packaging>
10+
<version>0.0.1-SNAPSHOT</version>
11+
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>3.3.2</version>
16+
<relativePath/>
17+
</parent>
18+
19+
<modules>
20+
<module>api</module>
21+
<module>domain</module>
22+
</modules>
23+
</project>

docker-modules/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
<module>docker-spring-boot</module>
1919
<module>docker-spring-boot-postgres</module>
2020
<module>docker-java-jar</module>
21+
<module>docker-multi-module-maven</module>
2122
<module>jib</module>
2223
</modules>
2324

24-
</project>
25+
</project>

0 commit comments

Comments
 (0)