Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
panwangnan
schbrain-parent
Commits
0e94596a
Commit
0e94596a
authored
May 14, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
e361754b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
31 deletions
+42
-31
integration/integration-maven-plugin/pom.xml
integration/integration-maven-plugin/pom.xml
+1
-7
integration/integration-maven-plugin/src/main/java/com/schbrain/maven/plugin/mojo/PrepareMojo.java
...main/java/com/schbrain/maven/plugin/mojo/PrepareMojo.java
+41
-24
No files found.
integration/integration-maven-plugin/pom.xml
View file @
0e94596a
...
@@ -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>
...
...
integration/integration-maven-plugin/src/main/java/com/schbrain/maven/plugin/mojo/PrepareMojo.java
View file @
0e94596a
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
target
File
)
{
private
Map
<
Object
,
Object
>
collectProjectVariables
(
Path
executableJar
File
)
{
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
(),
target
File
));
variables
.
put
(
"JAR_FILE"
,
getJarFileRelativePath
(
topProject
.
getBasedir
(),
executableJar
File
));
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
get
Target
File
(
String
finalName
,
File
targetDirectory
)
{
private
Path
get
ExecutableJar
File
(
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment