当前有一个系统用于导出项目,而每次导出的项目并不可以直接使用,需要手动从npm私人仓库中获取一个npm包然后将他们整合到一起它才是一个完整的项目,所以目前我的任务就是编写一个java程序可以自动地从npm私人仓库中拉取下来那个模板代码到指定地路径上去。并且我还要将这个java程序打包成jar包上传到先前创建好的maven私有仓库中去,这样可以在pom.xml中引入即可使用。
HttpClient
Apache HttpClient 是一个用于发送 HTTP 请求的客户端库,常用于 Java 应用程序中处理 HTTP 请求和响应。
有很多种方式可以让HttpClien连接你的项目,并依靠它去写代码。 最简单的方式也许就是使用 Maven 。你可以通过在你的 pom.xml 文件里的 标签中增加像下面这样的片段来完成这个整合。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version> <!-- 确保使用合适的版本 -->
</dependency>
这样之后,再在程序中将它引入即可以使用。
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
JAVA程序
接下来是编写java程序,我创建了以下代码的java程序。
package org.example;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class NpmDownloader {
private static final String REPO_URL = "http://localhost/repository/yb-npm/";
private static final String PACKAGE_NAME = "@yunbin/yunbin-ui";
private static final String VERSION = "1.0.0";
private static final String USERNAME = "admin";
private static final String PASSWORD = "admin123";
private static final String DOWNLOAD_DIR = "E:/ideaProjects/CloneNpm/src/main/directory"; // e.g., "C:/downloads"
public static void main(String[] args) {
try {
downloadNpmPackage(REPO_URL, PACKAGE_NAME, VERSION, USERNAME, PASSWORD, DOWNLOAD_DIR);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void downloadNpmPackage(String REPO_URL, String packageName, String version, String USERNAME, String PASSWORD, String DOWNLOAD_DIR) throws IOException {
String encodedPackageName = URLEncoder.encode(packageName, StandardCharsets.UTF_8.toString());
// 构建正确的包下载路径
String url = REPO_URL + encodedPackageName + "/-/" + packageName.substring(packageName.indexOf('/') + 1) + "-" + version + ".tgz";
// 创建下载目录(如果不存在)
File downloadDir = new File(DOWNLOAD_DIR);
if (!downloadDir.exists()) {
if (!downloadDir.mkdirs()) {
throw new IOException("Failed to create directory: " + DOWNLOAD_DIR);
}
}
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(USERNAME, PASSWORD));
try (CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultCredentialsProvider(credsProvider)
.build()) {
HttpGet request = new HttpGet(url);
try (CloseableHttpResponse response = client.execute(request)) {
if (response.getStatusLine().getStatusCode() == 200) {
try (InputStream inputStream = response.getEntity().getContent();
OutputStream outputStream = new FileOutputStream(DOWNLOAD_DIR + "/" + packageName.substring(packageName.indexOf('/') + 1) + "-" + version + ".tgz")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
System.out.println("Package downloaded successfully.");
} else {
System.out.println("Failed to download package. Status code: " +
response.getStatusLine().getStatusCode());
}
}
}
}
}
打包成jar包
直接运行mvn deploy就可以将该项目打包成jar包并上传到私人仓库中去。(maven的私人仓库地址和仓库认证如何配置就不在这里介绍。)
测试程序(如何使用)
创建了一个测试程序,在其中的pom.xml文件中引入已经打包好的jar包(引入的仓库地址配置在settings.xml中,在这里也不过多赘述。)
在程序中编写好以下代码,然后点击运行。
package org.example;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
String REPO_URL = "http://localhost:8081/repository/yb-npm/";
String PACKAGE_NAME = "@yunbin/yunbin-ui";
String VERSION = "1.0.0";
String USERNAME = "admin";
String PASSWORD = "admin123";
String DOWNLOAD_DIR = "E:/ideaProjects/testCloneNpm/src/main/directory"; // e.g., "C:/downloads"
NpmDownloader.downloadNpmPackage(REPO_URL,PACKAGE_NAME,VERSION,USERNAME,PASSWORD,DOWNLOAD_DIR);
}
}
即可看见在目标文件夹中拉取到了目标私人仓库中的代码,这里要注意,在设置DOWNLOAD_DIR的时候,所存的目标文件夹可以不存在,程序会自动创建该文件夹,根据该路径将拉取下来的npm包存储到该目标文件夹中,如果该文件夹里已有内容,程序会依据同名覆盖的原则进行覆盖。