Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
f4974216
Commit
f4974216
authored
Nov 18, 2018
by
fengshuonan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新test
parent
581ab8a2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
257 additions
and
182 deletions
+257
-182
src/main/java/cn/stylefeng/guns/MavenDeployLocalFile.java
+257
-0
src/main/java/cn/stylefeng/guns/Test.java
+0
-182
No files found.
src/main/java/cn/stylefeng/guns/MavenDeployLocalFile.java
0 → 100644
View file @
f4974216
package
cn
.
stylefeng
.
guns
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
MavenDeployLocalFile
{
private
List
<
File
>
directories
=
new
ArrayList
<>();
/**
* 当前工作目录
*/
private
String
CURRENT_PATH
=
"/Users/stylefeng/tmp/"
;
/**
* 仓库的地址
*/
private
String
REPO_PATH
=
"/Users/stylefeng/tmp/share"
;
/**
* maven的settings文件配置路径
*/
private
String
SETTINGS_CONFIG
=
"/Users/stylefeng/work/apache-maven-3.5.0/conf/settings.xml"
;
/**
* 仓库的名称
*/
private
String
REPOSITORY_ID
=
"maven-host-sedinBJ"
;
/**
* 仓库的url
*/
private
String
REPOSITORY_URL
=
"http://172.23.2.3:8081/repository/maven-host-sedinBJ/"
;
/**
* 递归获取一个目录下的所有文件目录路径
*
* @author fengshuonan
* @Date 2018/11/18 11:17 AM
*/
private
void
getAllDirs
(
String
fileDir
)
{
File
file
=
new
File
(
fileDir
);
File
[]
files
=
file
.
listFiles
();
if
(
files
==
null
)
{
return
;
}
for
(
File
item
:
files
)
{
if
(
item
.
isDirectory
())
{
directories
.
add
(
new
File
(
item
.
getAbsolutePath
()));
getAllDirs
(
item
.
getAbsolutePath
());
}
}
}
/**
* 获取目录的类型
*
* @author fengshuonan
* @Date 2018/11/18 11:21 AM
*/
private
DirectoryType
getDirectoryType
(
File
directoryPath
)
{
boolean
pom
=
false
;
boolean
jar
=
false
;
File
[]
files
=
directoryPath
.
listFiles
();
if
(
files
==
null
)
{
return
DirectoryType
.
NONE
;
}
for
(
File
file
:
files
)
{
if
(
file
.
getName
().
endsWith
(
".pom"
))
{
pom
=
true
;
}
else
if
(
file
.
getName
().
endsWith
(
".jar"
))
{
jar
=
true
;
}
}
if
(
pom
&&
!
jar
)
{
return
DirectoryType
.
POM
;
}
else
if
(
jar
&&
pom
)
{
return
DirectoryType
.
JAR_AND_POM
;
}
else
{
return
DirectoryType
.
NONE
;
}
}
/**
* 对只有pom文件的目录,执行mvn deploy操作
*
* @author fengshuonan
* @Date 2018/11/18 11:24 AM
*/
private
void
doOnlyPom
(
File
directory
)
{
File
[]
files
=
directory
.
listFiles
();
if
(
files
==
null
)
{
return
;
}
File
pom
=
null
;
for
(
File
file
:
files
)
{
String
name
=
file
.
getName
();
if
(
name
.
endsWith
(
".pom"
))
{
pom
=
file
;
}
}
String
command
=
buildComman
(
FileType
.
POM
,
pom
);
executeCommand
(
command
);
}
/**
* 对同时包含jar和pom文件的目录,执行mvn deploy操作
*
* @author fengshuonan
* @Date 2018/11/18 11:24 AM
*/
private
void
doJarAndPom
(
File
directory
)
{
File
[]
files
=
directory
.
listFiles
();
File
pom
=
null
;
File
jar
=
null
;
for
(
File
file
:
files
)
{
String
name
=
file
.
getName
();
if
(
name
.
endsWith
(
".pom"
))
{
pom
=
file
;
}
else
if
(
name
.
endsWith
(
".jar"
))
{
jar
=
file
;
}
}
if
(
pom
!=
null
)
{
String
command
=
buildComman
(
FileType
.
POM
,
pom
);
executeCommand
(
command
);
}
if
(
jar
!=
null
)
{
String
command
=
buildComman
(
FileType
.
JAR
,
jar
);
executeCommand
(
command
);
}
}
/**
* 程序入口
*
* @author fengshuonan
* @Date 2018/11/18 11:25 AM
*/
public
void
beginDeploy
()
{
//初始化,获取所有的目录存到list
this
.
getAllDirs
(
REPO_PATH
);
//遍历所有目录,并根据不同类型的目录,执行deploy
for
(
File
directory
:
directories
)
{
DirectoryType
directoryType
=
getDirectoryType
(
directory
);
if
(
directoryType
.
equals
(
DirectoryType
.
NONE
))
{
continue
;
}
else
if
(
directoryType
.
equals
(
DirectoryType
.
JAR_AND_POM
))
{
doJarAndPom
(
directory
);
}
else
if
(
directoryType
.
equals
(
DirectoryType
.
POM
))
{
doOnlyPom
(
directory
);
}
}
}
/**
* 执行comman命令
*
* @author fengshuonan
* @Date 2018/11/18 11:26 AM
*/
private
void
executeCommand
(
String
command
)
{
try
{
Process
exec
=
Runtime
.
getRuntime
().
exec
(
command
);
int
i
=
exec
.
waitFor
();
System
.
out
.
println
(
"执行结果:"
+
i
);
}
catch
(
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
/**
* 构造command命令
*
* @author fengshuonan
* @Date 2018/11/18 11:38 AM
*/
private
String
buildComman
(
FileType
fileType
,
File
deployFile
)
{
String
command
=
"mvn "
+
"-s "
+
SETTINGS_CONFIG
+
" "
+
"deploy:deploy-file "
+
"-Durl="
+
REPOSITORY_URL
+
" "
+
"-DrepositoryId="
+
REPOSITORY_ID
+
" "
;
String
absolutePath
=
deployFile
.
getAbsolutePath
();
String
leaveString
=
absolutePath
.
substring
(
0
,
absolutePath
.
lastIndexOf
(
"/"
));
//获取version
String
version
=
leaveString
.
substring
(
leaveString
.
lastIndexOf
(
"/"
)
+
1
);
leaveString
=
absolutePath
.
substring
(
0
,
leaveString
.
lastIndexOf
(
"/"
));
//获取artifactId
String
artifactId
=
leaveString
.
substring
(
leaveString
.
lastIndexOf
(
"/"
)
+
1
);
leaveString
=
absolutePath
.
substring
(
0
,
leaveString
.
lastIndexOf
(
"/"
));
//获取groupId
leaveString
=
leaveString
.
substring
(
CURRENT_PATH
.
length
());
String
groupId
=
leaveString
.
replaceAll
(
"/"
,
"."
);
//获取packing
String
packing
;
if
(
fileType
.
equals
(
FileType
.
JAR
))
{
packing
=
"-Dpackaging=jar "
;
}
else
{
packing
=
"-Dpackaging=pom "
;
}
command
+=
packing
;
command
+=
" -Dfile="
+
deployFile
.
getAbsolutePath
()
+
" "
;
command
+=
" -DgroupId="
+
groupId
+
" "
;
command
+=
" -DartifactId="
+
artifactId
+
" "
;
command
+=
" -Dversion="
+
version
+
" "
;
return
command
;
}
/**
* 目录的类型,什么都没,只有pom,同时包含jar和pom
*
* @author fengshuonan
* @Date 2018/11/18 11:36 AM
*/
private
enum
DirectoryType
{
NONE
,
POM
,
JAR_AND_POM
}
/**
* 文件类型
*
* @author fengshuonan
* @Date 2018/11/18 11:37 AM
*/
private
enum
FileType
{
JAR
,
POM
}
public
static
void
main
(
String
[]
args
)
{
new
MavenDeployLocalFile
().
beginDeploy
();
}
}
src/main/java/cn/stylefeng/guns/Test.java
deleted
100644 → 0
View file @
581ab8a2
package
cn
.
stylefeng
.
guns
;
import
lombok.Data
;
import
java.io.File
;
import
java.util.ArrayList
;
import
java.util.List
;
@Data
public
class
Test
{
private
List
<
File
>
directories
=
new
ArrayList
<>();
String
currentFilePath
=
"/Users/stylefeng/tmp/"
;
String
repoPath
=
"/Users/stylefeng/tmp/share"
;
private
void
getAllDirs
(
String
fileDir
)
{
File
file
=
new
File
(
fileDir
);
File
[]
files
=
file
.
listFiles
();
if
(
files
==
null
)
{
return
;
}
for
(
File
f
:
files
)
{
if
(
f
.
isDirectory
())
{
//System.out.println(f.getAbsolutePath());
directories
.
add
(
new
File
(
f
.
getAbsolutePath
()));
getAllDirs
(
f
.
getAbsolutePath
());
}
}
}
private
boolean
onlyPom
(
File
dir
)
{
boolean
pom
=
false
;
boolean
jar
=
false
;
File
[]
files
=
dir
.
listFiles
();
for
(
File
file
:
files
)
{
if
(
file
.
getName
().
endsWith
(
".pom"
))
{
pom
=
true
;
}
else
if
(
file
.
getName
().
endsWith
(
".jar"
))
{
jar
=
true
;
}
}
if
(
pom
&&
!
jar
)
{
return
true
;
}
else
{
return
false
;
}
}
private
boolean
jarAndPom
(
File
dir
)
{
boolean
pom
=
false
;
boolean
jar
=
false
;
File
[]
files
=
dir
.
listFiles
();
for
(
File
file
:
files
)
{
if
(
file
.
getName
().
endsWith
(
".pom"
))
{
pom
=
true
;
}
else
if
(
file
.
getName
().
endsWith
(
".jar"
))
{
jar
=
true
;
}
}
if
(
pom
&&
jar
)
{
return
true
;
}
else
{
return
false
;
}
}
private
boolean
none
(
File
dir
)
{
boolean
pom
=
false
;
boolean
jar
=
false
;
File
[]
files
=
dir
.
listFiles
();
for
(
File
file
:
files
)
{
if
(
file
.
getName
().
endsWith
(
".pom"
))
{
pom
=
true
;
}
else
if
(
file
.
getName
().
endsWith
(
".jar"
))
{
jar
=
true
;
}
}
if
(!
pom
&&
!
jar
)
{
return
true
;
}
else
{
return
false
;
}
}
private
void
doOnlyPom
(
File
directory
)
{
File
[]
files
=
directory
.
listFiles
();
File
pom
=
null
;
for
(
File
file
:
files
)
{
String
name
=
file
.
getName
();
if
(
name
.
endsWith
(
".pom"
))
{
pom
=
file
;
}
}
String
cmd
=
"mvn "
+
"-s /Users/stylefeng/work/apache-maven-3.5.0/conf/settings.xml "
+
"deploy:deploy-file "
+
"-Durl=http://172.23.2.3:8081/repository/maven-host-sedinBJ/ "
+
"-DrepositoryId=maven-host-sedinBJ "
;
String
absolutePath
=
pom
.
getAbsolutePath
();
//获取文件名
String
fileName
=
absolutePath
.
substring
(
absolutePath
.
lastIndexOf
(
"/"
)
+
1
);
String
other
=
absolutePath
.
substring
(
0
,
absolutePath
.
lastIndexOf
(
"/"
));
//获取version
String
version
=
other
.
substring
(
other
.
lastIndexOf
(
"/"
)
+
1
);
other
=
absolutePath
.
substring
(
0
,
other
.
lastIndexOf
(
"/"
));
//获取artifactId
String
artifactId
=
other
.
substring
(
other
.
lastIndexOf
(
"/"
)
+
1
);
other
=
absolutePath
.
substring
(
0
,
other
.
lastIndexOf
(
"/"
));
//获取groupId
other
=
other
.
substring
(
currentFilePath
.
length
());
String
groupId
=
other
.
replaceAll
(
"/"
,
"."
);
//获取packing
String
packing
=
"-Dpackaging=pom "
;
cmd
+=
packing
;
cmd
+=
" -Dfile="
+
pom
.
getAbsolutePath
()
+
" "
;
cmd
+=
" -DgroupId="
+
groupId
+
" "
;
cmd
+=
" -DartifactId="
+
artifactId
+
" "
;
cmd
+=
" -Dversion="
+
version
+
" "
;
System
.
out
.
println
(
cmd
);
System
.
out
.
println
();
}
private
void
doPomAndJar
(
File
directory
)
{
File
[]
files
=
directory
.
listFiles
();
File
pom
=
null
;
File
jar
=
null
;
for
(
File
file
:
files
)
{
String
name
=
file
.
getName
();
if
(
name
.
endsWith
(
".pom"
))
{
pom
=
file
;
}
else
if
(
name
.
endsWith
(
".jar"
))
{
jar
=
file
;
}
}
}
public
void
execute
()
{
Test
readFile
=
new
Test
();
readFile
.
getAllDirs
(
repoPath
);
List
<
File
>
directories
=
readFile
.
getDirectories
();
for
(
File
directory
:
directories
)
{
if
(
none
(
directory
))
{
continue
;
}
else
if
(
jarAndPom
(
directory
))
{
doPomAndJar
(
directory
);
}
else
if
(
onlyPom
(
directory
))
{
doOnlyPom
(
directory
);
}
}
}
public
static
void
main
(
String
[]
args
)
{
new
Test
().
execute
();
}
}
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