IntelliJ: How to add all jar files in a folder to classpath

喜欢而已 提交于 2019-12-04 05:29:12

问题


I recently opened a Maven project with the 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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

This pom file had to install Spring Framework. It was actually installed under ~/.m2/repository/org/springframework/. This is, of course, a huge folder that includes many jar files in many sub-folders. I want to add all jar files to classpath using IntelliJ. I want to ask how to do it. All I can do now is just adding one jar file after the other.


回答1:


1.) Folders can be added as class path in build, However the directory is expected to contain classes. Refer here for more details - Add directory in class path intelliji

2.) With respect to jars from local Maven repository, It can be achieved only by opening the Maven project in Intelliji using (Alt + F + O)

(Alt + F + O) and specify the path to pom.xml (the Maven project descriptor file). IntelliJ IDEA creates a project based on the Maven Project descriptor file

Ensure you congigure Maven settings in Intelliji at

File -> Settings -> Build, Execution, Deployment -> Maven

Give the same local repository path and maven settings file here.

Post which, do a mvn clean install -o . This build will pick up jars from local repository, which is already available. However, it may try and download pom files for the first time - ensure Internet is connected.

3.) Alternatively, You can do a find *.jar inside local maven repository path - Copy paste to an location and refer those jars in Intelliji as libraries.




回答2:


What finally solved this issue was re-opening a new Project by: File -> New -> Project -> Maven -> mark "Create from archetype" -> org.apache.maven.archetypes: maven-archetype-quickstart" -> ...



来源:https://stackoverflow.com/questions/41623110/intellij-how-to-add-all-jar-files-in-a-folder-to-classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!