gpt4 book ai didi

java - 从带有 Maven 的 Visual Studio Code for Java 开始

转载 作者:行者123 更新时间:2023-12-02 09:37:19 24 4
gpt4 key购买 nike

我又回到了 Java 开发的起点。我习惯于使用 Nuget 包在 C# 中工作。我对 Maven 有基本的概念。我想使用 VSC(Visual Studio Code)。

我正在研究如何导入我的第一个包以使用函数/方法 ByteUtils。看来这个方法来自 Apache 包。

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>testGroupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0</version>

<name>artifactId</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>

<dependency>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

以及问题的屏幕截图:

enter image description here

enter image description here

enter image description here

The container 'Maven Dependencies' references non existing library 'C:\Users\xxxx.m2\repository\org\apache\apache\21\apache-21.jar' Missing artifact org.apache:apache:jar:21

这里要完成的是我的app.java

package testGroupId;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class App
{
public static void main(String[] args) throws Exception
{
MessageDigest md = null;
try
{
md = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
System.out.println(e.getMessage());
}
//byte[] theDigest = md.digest(ByteUtils.intToBytesBigEndian(123456789))
System.out.println("Hello Java");
}
}

我的问题。为什么 org.apache 依赖项无法加载或下载?

最佳答案

Artifact org.apache:apache:pom:21不是 JAR Artifact ,而是 POM Artifact ,可以用作 Maven 模块的父级,也可以在 depencyManagement 部分中使用。

如果您添加 POM Artifact 作为正常依赖项,maven 将尝试下载该 Artifact 的 jar 文件,但它不存在,因此会出现错误。

添加为家长

<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
</parent>

将其添加到依赖管理中

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

如果你想org.apache.commons.compress.utils.ByteUtils那么你必须添加以下依赖项。

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>

关于java - 从带有 Maven 的 Visual Studio Code for Java 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57389955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com