Commit 0e94596a authored by liaozan's avatar liaozan 🏀

Polish

parent e361754b
...@@ -38,10 +38,6 @@ ...@@ -38,10 +38,6 @@
<version>${maven.plugin.version}</version> <version>${maven.plugin.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
...@@ -51,9 +47,7 @@ ...@@ -51,9 +47,7 @@
<artifactId>maven-plugin-plugin</artifactId> <artifactId>maven-plugin-plugin</artifactId>
<version>${maven.plugin.version}</version> <version>${maven.plugin.version}</version>
<configuration> <configuration>
<mojoDependencies> <requiredJavaVersion>${java.version}</requiredJavaVersion>
<param>com.schbrain.maven.plugin</param>
</mojoDependencies>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
......
package com.schbrain.maven.plugin.mojo; package com.schbrain.maven.plugin.mojo;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
...@@ -14,6 +11,7 @@ import java.io.IOException; ...@@ -14,6 +11,7 @@ import java.io.IOException;
import java.nio.file.*; import java.nio.file.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
/** /**
* @author liaozan * @author liaozan
...@@ -98,43 +96,46 @@ public class PrepareMojo extends AbstractMojo { ...@@ -98,43 +96,46 @@ public class PrepareMojo extends AbstractMojo {
return; return;
} }
validateParam();
Path targetFile = getTargetFile(finalName, buildDirectory);
if (!Files.exists(targetFile)) {
throw new MojoExecutionException("target jar is not present, it's required for build");
}
try { try {
Map<Object, Object> variables = collectProjectVariables(targetFile); validateParam();
storeVariablesToFile(variables);
getLog().info("generate build properties: \n" + JSONUtil.toJsonPrettyStr(variables)); Path executableJarFile = getExecutableJarFile(finalName, buildDirectory);
if (!Files.exists(executableJarFile)) {
throw new MojoExecutionException("target jar is not present, it's required for build");
}
Map<Object, Object> variables = collectProjectVariables(executableJarFile);
String content = storeVariablesToFile(variables);
getLog().info("generated build properties: \n" + content);
} catch (Exception e) { } catch (Exception e) {
throw new MojoExecutionException(e); String errorMsg = String.format("%s: %s", e.getClass().getName(), e.getMessage());
getLog().error(errorMsg);
throw new MojoExecutionException(errorMsg, e);
} }
} }
private void validateParam() throws MojoExecutionException { private void validateParam() throws MojoExecutionException {
MavenProject topLevelProject = session.getTopLevelProject(); MavenProject topLevelProject = session.getTopLevelProject();
if (StrUtil.isBlank(dockerRegistry)) { if (isBlank(dockerRegistry)) {
throw new MojoExecutionException("docker.registry is required for build, but found empty value, please check the pom configuration"); throw new MojoExecutionException("docker.registry is required for build, but found empty value, please check the pom configuration");
} }
if (StrUtil.isBlank(springProfile)) { if (isBlank(springProfile)) {
throw new MojoExecutionException("spring.profile is required for build, but found empty value, please check the pom configuration"); throw new MojoExecutionException("spring.profile is required for build, but found empty value, please check the pom configuration");
} }
if (StrUtil.isBlank(appName)) { if (isBlank(appName)) {
appName = topLevelProject.getArtifactId(); appName = topLevelProject.getArtifactId();
} }
if (StrUtil.isBlank(version)) { if (isBlank(version)) {
version = topLevelProject.getVersion(); version = topLevelProject.getVersion();
} }
} }
private Map<Object, Object> collectProjectVariables(Path targetFile) { private Map<Object, Object> collectProjectVariables(Path executableJarFile) {
MavenProject topProject = session.getTopLevelProject(); MavenProject topProject = session.getTopLevelProject();
Map<Object, Object> variables = new HashMap<>(); Map<Object, Object> variables = new HashMap<>();
// required for docker build // required for docker build
variables.put("APP_NAME", appName); variables.put("APP_NAME", appName);
variables.put("JAR_FILE", getJarFileRelativePath(topProject.getBasedir(), targetFile)); variables.put("JAR_FILE", getJarFileRelativePath(topProject.getBasedir(), executableJarFile));
variables.put("VERSION", version); variables.put("VERSION", version);
variables.put("REGISTRY", dockerRegistry); variables.put("REGISTRY", dockerRegistry);
variables.put("PROFILE", springProfile); variables.put("PROFILE", springProfile);
...@@ -153,11 +154,14 @@ public class PrepareMojo extends AbstractMojo { ...@@ -153,11 +154,14 @@ public class PrepareMojo extends AbstractMojo {
return variables; return variables;
} }
private void storeVariablesToFile(Map<Object, Object> variables) throws IOException { private String storeVariablesToFile(Map<Object, Object> variables) throws IOException {
Path dockerBuildInfo = Paths.get(buildDirectory.getAbsolutePath(), DOCKER_BUILD_INFO); Path dockerBuildInfo = Paths.get(buildDirectory.getAbsolutePath(), DOCKER_BUILD_INFO);
FileUtil.del(dockerBuildInfo); if (Files.exists(dockerBuildInfo)) {
Files.createFile(dockerBuildInfo); Files.delete(dockerBuildInfo);
FileUtil.writeUtf8Map(variables, dockerBuildInfo.toFile(), "=", false); }
String content = variablesToString(variables);
Files.writeString(dockerBuildInfo, content);
return content;
} }
private String getJarFileRelativePath(File basedir, Path jarFile) { private String getJarFileRelativePath(File basedir, Path jarFile) {
...@@ -172,7 +176,7 @@ public class PrepareMojo extends AbstractMojo { ...@@ -172,7 +176,7 @@ public class PrepareMojo extends AbstractMojo {
return false; return false;
} }
private Path getTargetFile(String finalName, File targetDirectory) { private Path getExecutableJarFile(String finalName, File targetDirectory) {
return Paths.get(targetDirectory.getPath(), withExtension(finalName)); return Paths.get(targetDirectory.getPath(), withExtension(finalName));
} }
...@@ -180,4 +184,17 @@ public class PrepareMojo extends AbstractMojo { ...@@ -180,4 +184,17 @@ public class PrepareMojo extends AbstractMojo {
return String.format("%s.%s", fileName, project.getArtifact().getArtifactHandler().getExtension()); return String.format("%s.%s", fileName, project.getArtifact().getArtifactHandler().getExtension());
} }
private boolean isBlank(String value) {
return value == null || value.isBlank();
}
private String variablesToString(Map<Object, Object> variables) {
StringBuilder builder = new StringBuilder();
for (Entry<Object, Object> entry : variables.entrySet()) {
builder.append(entry.getKey()).append("=").append(entry.getValue());
builder.append(System.lineSeparator());
}
return builder.deleteCharAt(builder.length() - 1).toString();
}
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment