需求:本地aar文件上传到maven库
参考我之前的博客gradle上传本地文件到远程maven库(nexus服务器)
下面是java图形化工具代码
package com.jinkejoy.build_aar; import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField; public class UploadAarFile {
private JFrame jFrame;
private JTextField aar_path;
private JButton aarPath_button;
private File aar_File;
private JTextField main_path;
private JButton main_button;
private File main_File;
private JTextField groupId;
private JTextField artifactId;
private JTextField version;
private JButton upload; public static void main(String[] args) {
new UploadAarFile();
} public UploadAarFile() {
openFileWindow();
} private void openFileWindow() {
jFrame = new JFrame();
jFrame.setTitle("将aar上传到maven库");
jFrame.setBounds(500, 500, 700, 160);
jFrame.setVisible(true);
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
//选择aar文件
JLabel filePath_label = new JLabel("aar本地路径:");
aar_path = new JTextField(48);
aarPath_button = new JButton("浏览");
//前缀包名
JLabel groupId_label = new JLabel("前缀包名:");
groupId = new JTextField(25);
//aar名
JLabel aar_label = new JLabel("aar文件名:");
artifactId = new JTextField(25);
//版本
JLabel version_label = new JLabel("aar版本号:");
version = new JTextField(25);
//母包
JLabel main_label = new JLabel("母包路径:");
main_path = new JTextField(50);
main_button = new JButton("浏览");
//上传
upload = new JButton("上传");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLayout(layout);
jFrame.setResizable(false);
jFrame.add(main_label);
jFrame.add(main_path);
jFrame.add(main_button);
jFrame.add(filePath_label);
jFrame.add(aar_path);
jFrame.add(aarPath_button);
jFrame.add(groupId_label);
jFrame.add(groupId);
jFrame.add(aar_label);
jFrame.add(artifactId);
jFrame.add(version_label);
jFrame.add(version);
jFrame.add(upload);
findAarFile();
findMainFile();
uploadAar();
} private void findMainFile() {
main_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.showDialog(new JLabel(), "选择");
main_File = chooser.getSelectedFile();
main_path.setText(main_File.getAbsolutePath().toString());
}
});
} private void findAarFile() {
aarPath_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.showDialog(new JLabel(), "选择");
aar_File = chooser.getSelectedFile();
aar_path.setText(aar_File.getAbsolutePath().toString());
}
});
} private void uploadAar() {
upload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
uploadAarImpl();
}
});
} private void uploadAarImpl() {
if (checkInput()) return;
cachePath();
gradleUpload();
} private void cachePath() {
String cache = "mainPath=" + main_path.getText().toString().replace("\\", "\\\\") + "\n" +
"aarPath=" + aar_path.getText().toString().replace("\\", "\\\\") + "\n" +
"groupId=" + groupId.getText().toString().replace("\\", "\\\\") + "\n" +
"artifactId=" + artifactId.getText().toString().replace("\\", "\\\\") + "\n" +
"version=" + version.getText().toString().replace("\\", "\\\\");
File cacheFile = new File(main_path.getText().toString() + "/aarParam.properties");
if (!cacheFile.exists()) {
try {
cacheFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fop = new FileOutputStream(cacheFile);
fop.write(cache.getBytes());
fop.flush();
fop.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} private void gradleUpload() {
String command = "cmd /c start gradlew clean uploadArchives";
File cmdPath = new File(main_path.getText().toString());
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(command, null, cmdPath);
} catch (IOException e) {
e.printStackTrace();
}
} private boolean checkInput() {
if ("".equals(aar_path.getText().toString())) {
JOptionPane.showMessageDialog(jFrame, "请输入aar文件路径");
return true;
}
if ("".equals(main_path.getText().toString())) {
JOptionPane.showMessageDialog(jFrame, "请输入母包路径");
return true;
}
if ("".equals(groupId.getText().toString())) {
JOptionPane.showMessageDialog(jFrame, "请输入前缀包名");
return true;
}
if ("".equals(artifactId.getText().toString())) {
JOptionPane.showMessageDialog(jFrame, "请输入aar名称");
return true;
}
if ("".equals(version.getText().toString())) {
JOptionPane.showMessageDialog(jFrame, "请输入aar版本号");
return true;
}
return false;
}
}
jar包打成可运行程序参考android studio打可执行jar包
效果图