0

I created Maven project with IntelliJ IDEA, and followed this tutorial on how to connect PostgreSQL JDBC with Java. But, instead of changing pom.xml file, I went to Project StructureLibraries, where I've added postgresql-42.6.0.jar.

Everything works fine, but I'm curious how is this dependency stored, since pom.xml file does not include postgresql-42.6.0.jar.

<?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>dbproject</artifactId>
    <version>1.0-SNAPSHOT</version>

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

</project>

2 Answers2

0

By default .m2/repository from the user's home directory is used. To change it you can select the library destination while downloading:

enter image description here

In general, it is not recommended to manage dependencies through Project StructureLibraries if build system like Maven/Gradle is used in the project because these settings might be lost after the project re-import triggered by the IDE Maven plugin.

Consider adding this dependency to the pom.xml directly.

Egor Klepikov
  • 3,386
  • 5
  • 19
  • 34
0

IntelliJ load the dependency based on your pom.xml and add it to the Libraries automatically.

Manually adding jars to the Libraries will work. Still, it will only work in the machine where the jar is added, and every time the project is loaded into another machine or reloaded in the same machine, the jar has to be added manually.

You must use Maven for dependency management so this manual action can be avoided. And your project will work in all places, including other IDEs and CI servers.

If you want to add the PostgreSQL JDBC driver to your project, add the following dependency in your pom.xml in the dependencies section.

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.6.0</version>
</dependency>

So your final pom.xml will be

<?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>dbproject</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
      <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.6.0</version>
      </dependency>
    </dependencies>

</project>

seenukarthi
  • 8,241
  • 10
  • 47
  • 68