简体   繁体   English

从终端编译时Maven项目缺少包

[英]Maven project is missing package when compiling from terminal

I'm currently trying to compile my first maven project from the command line but whenever I run this command:我目前正在尝试从命令行编译我的第一个 Maven 项目,但是每当我运行此命令时:

C:\Users\zacha\git\INF2050\tp1\PELZ07039904\src\main\java>javac analyse.java

I get this back analyse.java:7: error: package org.apache.commons.io does not exist我得到这个回analyse.java:7: error: package org.apache.commons.io does not exist

But the project runs perfectly fine when I run it directly from InteliJ但是当我直接从 InteliJ 运行它时,该项目运行得非常好

Here is my pom.xml file:这是我的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>org.example</groupId>
    <artifactId>PELZ07039904</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>

        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>

</project>

I checked and all my jar are in my .m2 file so I'm not sure why I'm getting this error.我检查了一下,我所有的 jar 都在我的 .m2 文件中,所以我不确定为什么我会收到这个错误。

javac doesn't have any idea about maven and it's dependencies. javac 对 maven 及其依赖关系一无所知。 All it can do is just compile a class.它所能做的只是编译一个类。 Thats it.就是这样。 Instead you need to use maven specific command.相反,您需要使用 maven 特定命令。

Maven provides a command line tool. Maven 提供了一个命令行工具。

To build a Maven project via the command line, run the mvn command from the command line.要通过命令行构建 Maven 项目,请从命令行运行 mvn 命令。 The command should be executed in the directory which contains the relevant pom file.该命令应在包含相关 pom 文件的目录中执行。 You need to provide the mvn command with the life cycle phase or goal to execute.您需要为 mvn 命令提供生命周期阶段或要执行的目标。

The Maven tooling reads the pom file and resolves the dependencies of the project. Maven 工具读取 pom 文件并解析项目的依赖项。 Maven validates if required components are available in a local repository. Maven 验证本地存储库中是否有所需的组件。 The local repository is found in the .m2/repository folder of the users home directory.本地存储库位于用户主目录的 .m2/repository 文件夹中。

For example the mvn clean install command triggers the jar packaging.例如 mvn clean install 命令触发 jar 打包。 This includes compiling the sources, executing the tests and packaging the compiled files in a JAR file.这包括编译源代码、执行测试并将编译后的文件打包到 JAR 文件中。 As last step the install phase installs the resulting artifact into the local repository, so it can be used as dependencies by other Maven builds.作为最后一步,安装阶段将生成的工件安装到本地存储库中,因此它可以用作其他 Maven 构建的依赖项。

Check full guide here 在此处查看完整指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM