简体   繁体   中英

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:

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

But the project runs perfectly fine when I run it directly from InteliJ

Here is my pom.xml file:

<?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.

javac doesn't have any idea about maven and it's dependencies. All it can do is just compile a class. Thats it. Instead you need to use maven specific command.

Maven provides a command line tool.

To build a Maven project via the command line, run the mvn command from the command line. The command should be executed in the directory which contains the relevant pom file. You need to provide the mvn command with the life cycle phase or goal to execute.

The Maven tooling reads the pom file and resolves the dependencies of the project. Maven validates if required components are available in a local repository. The local repository is found in the .m2/repository folder of the users home directory.

For example the mvn clean install command triggers the jar packaging. This includes compiling the sources, executing the tests and packaging the compiled files in a JAR file. 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.

Check full guide here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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