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

标签 java maven visual-studio-code

我又回到了 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/

相关文章:

java - 如何在 Maven mojo 中使用 MavenBuildHelper

java - 如何将其他类路径依赖项传递给 Maven 中的 testCompile 阶段

android - 未找到设备 Flutter VSCode 模拟器

javascript - 从 2.7.x 升级到 2.8.x 后出现 TypeScript 错误 TS2349

java - 在 2.3.4 中序列化 DecimalFormatSymbols 时出现问题

java - Intellij Idea CE 2018.2.1 中资源目录不包含在输出目录中

windows - Windows 进程保持 mvn clean 失败

c - VSCode C/C++ Intellisense 问题与 netinet/ip.h

java - 使用 selenium 中的机器人类上传文件在远程计算机上工作正常,但在远程连接关闭时失败

java - 将 JSR 303 Java Bean Validation 异常转换为自定义异常