Add External Library . JAR, to MAVEN project using VSCODE?

Asked

Viewed 1,394 times

2

I am developing a JAVA project together with MAVEN to be compiled. However, I need to put the library jar. that is already on my computer in the MAVEN project and pull the data that is inside it to serve as a complement. Like Netbeans, Eclipse, and other IDE that has support for this.

I am starting to use VSCODE, but I know little of the features and tools the IDE provides. I already installed the MAVEN and the artifacts, already configured the environment variables. But adding a dependency that’s already on my computer is what I can’t. I just need to put the library into my project and use it.

1 answer

3


For cases where the library is customized, and not available in any repository (internal company or internet), you must install the JAR locally (for its development).

Installation in the local repository can be done using the following syntax at the command prompt:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

If the JAR was created using Maven 2.5, Voce can simply execute the following command that Maven will recognize pom.xml that is inside the JAR and install it correctly:

mvn install:install-file -Dfile=<path-to-file>

After installing the JAR you can use the dependency on your POM normally.

IMPORTANT: Remember that as the JAR appears in your LOCAL repository, in case you try to build using another tool outside your local environment (Jenkins, Bamboo...) JAR will not be available, you should remember to install it also in the repository read by the tools.

You can check the documentation here.


UPDATE: I’m supplementing the answer based on your other questions.

An example of how to install a new JAR:

  • Using your the library jar. as an example:

mvn install:install-file -Dfile="D:\Usuarios\ngueno\Desktop\biblioteca.jar" -DgroupId="com.ngueno" -DartifactId="biblioteca" -Dversion="1.0.0" -Dpackaging="jar"

Where:

file - Path to the JAR to be installed

groupId: Artifact-related group, usually defined by the company (a default is to use the company’s website in reverse)

artifactId: Name/Identifier of the artifact

version: Artifact version (used for releases/snapshots)

packaging: Packaging type (JAR, WAR, EAR...)

Running the installation command will give the following result:

λ mvn install:install-file -Dfile="D:\Usuarios\ngueno\Desktop\biblioteca.jar" -DgroupId="com.ngueno" -DartifactId="biblioteca" -Dversion="1.0.0" -Dpackaging="jar"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom ---
[INFO] Installing D:\Usuarios\ngueno\Desktop\biblioteca.jar to C:\Users\ngueno\.m2\repository\com\ngueno\biblioteca\1.0.0\biblioteca-1.0.0.jar
[INFO] Installing C:\Users\ngueno\AppData\Local\Temp\mvninstall2369252046556360803.pom to C:\Users\ngueno\.m2\repository\com\ngueno\biblioteca\1.0.0\biblioteca-1.0.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.537 s
[INFO] Finished at: 2019-01-17T09:47:15-02:00
[INFO] ------------------------------------------------------------------------

Note that the JAR was installed in the local repository in the log generated by Maven:

[INFO] Installing D:\Usuarios\ngueno\Desktop\biblioteca.jar to C:\Users\ngueno\.m2\repository\com\ngueno\biblioteca\1.0.0\biblioteca-1.0.0.jar

And was also generated a pom for the artifact:

[INFO] Installing C:\Users\ngueno\AppData\Local\Temp\mvninstall2369252046556360803.pom to C:\Users\ngueno\.m2\repository\com\ngueno\biblioteca\1.0.0\biblioteca-1.0.0.pom

For more information on the dependency structure, take a look at this Maven documentation and in this Overview about the Maven.

  • I have to put in the <group-id> <Artifact-id> <version> <Packaging> the same when I created the Maven?

  • No, when installing you will enter the JAR information that you are installing, you can set any name. You will use this information to include the dependency installed on pom.xml of your current project.

  • What is the packagin for? And what should I put in it? Is that I’m new to Maven

  • 1

    @System Complementing the explanation in the reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.