Skip to content

Commit

Permalink
Merge pull request #554 from swarajsaaj/add_jib
Browse files Browse the repository at this point in the history
Add jib
  • Loading branch information
pascalgrimaud authored Jan 20, 2022
2 parents 6243a32 + 6358a7a commit c38fa9d
Show file tree
Hide file tree
Showing 16 changed files with 493 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ public void checkFolder() {
}
}

public int getServerPort() {
int serverPort;
try {
serverPort = this.getIntegerConfig("serverPort").orElse(8080);
} catch (UnauthorizedValueException e) {
serverPort = 8080;
}
return serverPort;
}

/**
* Attempts detection of end-of-line characters by reading files at the root of the project folder or defaults to \n.
* Non-UTF-8 files can lead to unexpected results.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tech.jhipster.lite.generator.server.springboot.docker.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.springboot.docker.domain.SpringBootDockerService;

@Service
public class SpringBootDockerApplicationService {

private final SpringBootDockerService springBootDockerService;

public SpringBootDockerApplicationService(SpringBootDockerService springBootDockerService) {
this.springBootDockerService = springBootDockerService;
}

public void addJib(Project project) {
this.springBootDockerService.addJib(project);
}

public void addJibPlugin(Project project) {
this.springBootDockerService.addJibPlugin(project);
}

public void addJibFiles(Project project) {
this.springBootDockerService.addJibFiles(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tech.jhipster.lite.generator.server.springboot.docker.domain;

public class SpringBootDocker {

private static final String DOCKER_BASE_IMAGE = "eclipse-temurin:17-jre-focal";
private static final String DOCKER_PLATFORM_ARCHITECTURE = "amd64";
private static final String JIB_PLUGIN_VERSION = "3.1.4";

private SpringBootDocker() {}

public static String getDockerBaseImage() {
return DOCKER_BASE_IMAGE;
}

public static String getDockerPlatformArchitecture() {
return DOCKER_PLATFORM_ARCHITECTURE;
}

public static String getJibPluginVersion() {
return JIB_PLUGIN_VERSION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package tech.jhipster.lite.generator.server.springboot.docker.domain;

import static tech.jhipster.lite.common.domain.FileUtils.getPath;

import tech.jhipster.lite.common.domain.WordUtils;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.buildtool.generic.domain.Plugin;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

public class SpringBootDockerDomainService implements SpringBootDockerService {

public static final String SOURCE = "server/springboot/docker/jib";

private final ProjectRepository projectRepository;
private final BuildToolService buildToolService;

public SpringBootDockerDomainService(ProjectRepository projectRepository, BuildToolService buildToolService) {
this.projectRepository = projectRepository;
this.buildToolService = buildToolService;
}

@Override
public void addJib(Project project) {
this.addJibPlugin(project);
this.addJibFiles(project);
}

@Override
public void addJibFiles(Project project) {
String baseName = project.getBaseName().orElse("jhipster");
String packageName = project.getPackageName().orElse("com.mycompany.myapp");
String className = WordUtils.upperFirst(baseName);
project.addConfig("mainClass", packageName + "." + className + "App");
projectRepository.template(project, getPath(SOURCE), "entrypoint.sh", getPath("src/main/docker/jib"));
}

@Override
public void addJibPlugin(Project project) {
Plugin jibPlugin = Plugin
.builder()
.groupId("com.google.cloud.tools")
.artifactId("jib-maven-plugin")
.version("\\${jib-maven-plugin.version}")
.additionalElements(
"""
<configuration>
<from>
<image>\\${jib-maven-plugin.image}</image>
<platforms>
<platform>
<architecture>\\${jib-maven-plugin.architecture}</architecture>
<os>linux</os>
</platform>
</platforms>
</from>
<to>
<image>%s:latest</image>
</to>
<container>
<entrypoint>
<shell>bash</shell>
<option>-c</option>
<arg>/entrypoint.sh</arg>
</entrypoint>
<ports>
<port>%s</port>
</ports>
<environment>
<SPRING_OUTPUT_ANSI_ENABLED>ALWAYS</SPRING_OUTPUT_ANSI_ENABLED>
<JHIPSTER_SLEEP>0</JHIPSTER_SLEEP>
</environment>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
<user>1000</user>
</container>
<extraDirectories>
<paths>src/main/docker/jib</paths>
<permissions>
<permission>
<file>/entrypoint.sh</file>
<mode>755</mode>
</permission>
</permissions>
</extraDirectories>
</configuration>""".formatted(
project.getBaseName().orElse("jhipster"),
project.getServerPort()
)
)
.build();
this.buildToolService.addProperty(project, "jib-maven-plugin.version", SpringBootDocker.getJibPluginVersion());
this.buildToolService.addProperty(project, "jib-maven-plugin.image", SpringBootDocker.getDockerBaseImage());
this.buildToolService.addProperty(project, "jib-maven-plugin.architecture", SpringBootDocker.getDockerPlatformArchitecture());
this.buildToolService.addPlugin(project, jibPlugin);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tech.jhipster.lite.generator.server.springboot.docker.domain;

import tech.jhipster.lite.generator.project.domain.Project;

public interface SpringBootDockerService {
void addJib(Project project);
void addJibFiles(Project project);
void addJibPlugin(Project project);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tech.jhipster.lite.generator.server.springboot.docker.infrastructure.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.server.springboot.docker.domain.SpringBootDockerDomainService;
import tech.jhipster.lite.generator.server.springboot.docker.domain.SpringBootDockerService;

@Configuration
public class SpringBootDockerBeanConfiguration {

private final ProjectRepository projectRepository;
private final BuildToolService buildToolService;

public SpringBootDockerBeanConfiguration(ProjectRepository projectRepository, BuildToolService buildToolService) {
this.projectRepository = projectRepository;
this.buildToolService = buildToolService;
}

@Bean
public SpringBootDockerService springBootDockerService() {
return new SpringBootDockerDomainService(projectRepository, buildToolService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tech.jhipster.lite.generator.server.springboot.docker.infrastructure.primary.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.springboot.docker.application.SpringBootDockerApplicationService;

@RestController
@RequestMapping("/api/servers/spring-boot/docker")
@Tag(name = "Spring Boot - Tools")
class SpringBootDockerResource {

private final SpringBootDockerApplicationService springBootDockerApplicationService;

public SpringBootDockerResource(SpringBootDockerApplicationService springBootDockerApplicationService) {
this.springBootDockerApplicationService = springBootDockerApplicationService;
}

@Operation(summary = "Add docker image building with Jib")
@ApiResponse(responseCode = "500", description = "An error occurred while adding jib")
@PostMapping("/jib")
public void addJib(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
springBootDockerApplicationService.addJib(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.server.springboot.docker;
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import static tech.jhipster.lite.generator.server.springboot.mvc.web.domain.SpringBootMvc.*;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.jhipster.lite.error.domain.UnauthorizedValueException;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
Expand All @@ -18,8 +15,6 @@

public class SpringBootMvcDomainService implements SpringBootMvcService {

private final Logger log = LoggerFactory.getLogger(SpringBootMvcDomainService.class);

public static final String SOURCE = "server/springboot/mvc/web";
public static final String EXCEPTION_HANDLER_PATH = "technical/infrastructure/primary/exception";

Expand Down Expand Up @@ -113,23 +108,12 @@ private void templateToExceptionHandler(Project project, String source, String t
}

private void addServerPortInProperties(Project project) {
springBootCommonService.addProperties(project, "server.port", getServerPort(project));
springBootCommonService.addProperties(project, "server.port", project.getServerPort());
springBootCommonService.addPropertiesTest(project, "server.port", 0);
}

private void addLoggerInConfiguration(Project project, String packageName, Level level) {
springBootCommonService.addLogger(project, packageName, level);
springBootCommonService.addLoggerTest(project, packageName, level);
}

private int getServerPort(Project project) {
int serverPort;
try {
serverPort = project.getIntegerConfig("serverPort").orElse(8080);
} catch (UnauthorizedValueException e) {
log.warn("The serverPort config is not valid");
serverPort = 8080;
}
return serverPort;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

echo "The application will start in ${JHIPSTER_SLEEP}s..." && sleep ${JHIPSTER_SLEEP}
exec java ${JAVA_OPTS} -noverify -XX:+AlwaysPreTouch -Djava.security.egd=file:/dev/./urandom -cp /app/resources/:/app/classes/:/app/libs/* "{{mainClass}}" "$@"
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,32 @@ void shouldNotGetIntegerConfig() {
}
}

@Nested
class GetServerPortTest {

@Test
void shouldGetDefaultServerPort() {
Project project = tmpProject();
assertThat(project.getServerPort()).isEqualTo(8080);
}

@Test
void shouldGetPresetServerPort() {
Project project = tmpProject();
project.addConfig("serverPort", 1337);
assertThat(project.getServerPort()).isEqualTo(1337);
}

@Test
void shouldGetDefaultServerPortForInvalidProperty() {
Project project = tmpProject();

project.addConfig("serverPort", List.of(1337));

assertThat(project.getServerPort()).isEqualTo(8080);
}
}

@Test
void shouldBeMavenProject() throws Exception {
Project project = Project.builder().folder(tmpDirForTest()).build();
Expand Down
Loading

0 comments on commit c38fa9d

Please sign in to comment.