In the tool project commonly used in interface development, I want to call cmd in java to implement some commands, here is to import an existing jar package into maven for management
code
public static String addJar2Maven(String path, String groupId, String artifactId,
String versionId, String userName) {
String result;
StringBuffer sb = new StringBuffer();
try {
sb.append("cmd /c C: cd User\\").append(userName).append(" && mvn install:install-file -DgroupId=").append(groupId)
.append(" -DartifactId=").append(artifactId).append(" -Dversion=")
.append(versionId).append(" -Dpackaging=jar -Dfile=").append(path);
System.out.println(sb.toString());
Process process = Runtime.getRuntime().exec(sb.toString());
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String msg;
while ((msg = reader.readLine()) != null)
System.out.println(msg);
reader.close();
result = ">>> add jar to maven success!";
} catch (IOException e) {
result = ">>> add jar to maven fail: " + e.getMessage();
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
//TODO 1.jar包路径
String path = "D:\\...\\xxx.jar";
//TODO 2.jar包groupid
String groupId = "com.xxx";
//TODO 3.jar包artifactid
String artifactId = "xxx";
//TODO 4.jar包versionid
String versionId = "xxx";
//TODO 5.window当前用户名
String userName = "xxx";
System.out.println(AddJar2Maven.addJar2Maven(path, groupId, artifactId, versionId,userName));
}
Modify 5 TODOs according to your own situation, and then run the main method to complete