Maven java: package does not exist - java

I am making a discord bot in java with JDA and I wanted to try to host it. I found out the best way to build an IntelliJ project is with maven. If I run mvn install i get the following error.
> java: package net.dv8tion.jda.api does not exist
I searched stack overflow and think I need to add a dependency but because this is my first time using java I really can't find out how to implement it in my project. Can anyone tell me how to fix the problem and how to implement it?
Here is my pom 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>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>bananaaction</groupId>
<artifactId>DiscordBot</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_222</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId> <!-- In Red -->
<configuration>
<archive>
<manifest>
<mainClass>com.bananaaction.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

As you guessed, the problem is that you didn't include JDA as a dependency (library).
In order to do this, add this to the pom.xml:
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_222</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
See the README of JDA for details.
The first section (<dependencies>) declares your dependencies (libraries).
Here, you add a dependency with the group id net.dv8tion and artifact id JDA with version 4.2.0_222 (the latest version at the time of writing this (may 2nd, 2020).
The second section (<repositories>) defines where to look for. JDA is not in the maven central (the default) repository but in the jcenter repository so you need to add the jcenter repository to your pom.xml.
If you package it to a JAR, dependencies will not be included. In order to do this, you can use the maven-assembly-plugin, for example.
You can add the plugin to the pom.xml wth the following code in the <plugins> secion:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>yourPackage.yourClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Integrated in your pom.xml, it could look like this:
<?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>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>com.bananaaction</groupId>
<artifactId>IdeaProjects</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_222</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>yourPackage.yourClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Don't forget to change the main class, however.

Related

How to prevent specifying the versions of sibling modules in maven-assembly-plugin?

The simplified setup of my project is as follows:
root
|--parent
|--service-1
|--service-2
|--service-aggregator
I need to assemble the 'service-1' & 'service-2' modules in 'service-aggregator'. I am using maven-assembly-plugin for the same and it works fine, however I foresee a maintenance issue wherein whenever the version of either of service-1 or service-2 will change, I will need to update the service-aggregator pom.xml also.
Hence, I am looking for a way to prevent writing the version of the service-1 / -2 in the service-aggregator pom.xml i.e. I just need service-aggregator to simply pick the latest version of service-1 / -2.
I have gone through the examples provided in maven-assembly-plugin documentation but those contain the version being mentioned in the assembly module (service-aggregator in my example).
Please let me know if any detail is missing & I will add it.
Here is the key contents of service-aggregator pom.xml:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company.project</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.company.project.parent</groupId>
<artifactId>service-aggregator</artifactId>
<name>service-aggregator</name>
<dependencies>
<dependency>
<groupId>com.company.project.parent</groupId>
<artifactId>service-1</artifactId>
<version>0.0.1</version> <!-- this is the line troubling me -->
</dependency>
<dependency>
<groupId>com.company.project.parent</groupId>
<artifactId>service-2</artifactId>
<version>0.0.1</version> <!-- this is the line troubling me -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<skipAssembly>${skip.assembly}</skipAssembly>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<skip.assembly>false</skip.assembly>
</properties>
The best thing is to use properties like this:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company.project</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.company.project.parent</groupId>
<artifactId>service-aggregator</artifactId>
<name>service-aggregator</name>
<dependencies>
<dependency>
<groupId>com.company.project.parent</groupId>
<artifactId>service-1</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.company.project.parent</groupId>
<artifactId>service-2</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<skipAssembly>${skip.assembly}</skipAssembly>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Microsoft Event hub send java

I am following this tutorial on Microsoft website to get a simple send event to Azure event hub to work in java. However, I used maven and followed every thing but the final jar file runs with exception errors. Please here is what I did.
I created simple Maven project in Eclipse Juno and here is the content of my pom.xml
`<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">
<description>Java libraries for talking to Windows Azure Event Hubs</description>
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure</groupId>
<artifactId>send</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.bd.Send</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-eventhubs</artifactId>
<version>0.14.0</version>
</dependency>
</dependencies>
</project>`
Is this pom.xml file correct? as when I run the final jar file, i get exception errors but the project compiles correctly.
How can I configure the send event hub java project using maven ??
Thanks in advance
This is the pom.xml file that worked:
<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>send</groupId>
<artifactId>send</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>osssonatype</id>
<name>OSS Sonatype</name>
<url>https://repo1.maven.org/maven2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-eventhubs</artifactId>
<version>0.14.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>sdasf.Send</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
When Run as Maven install. Inside the target folder there will be 2 jar files. Running the one with dependencies solved my issue.
on the cmd command line:
java -jar send-0.0.1-SNAPSHOT-jar-with-dependencies.jar

Why am I getting this Maven error?

I've been trying to fix this problem for a good part of an hour now, and I haven't gotten anywhere.
When I attempt to compile my project using Maven, I get this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (default-cli) on project GankALane: Unable to parse configuration of mojo org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single for parameter archive: Cannot find setter, adder nor field in org.apache.maven.archiver.MavenArchiveConfiguration for 'descriptorRefs'
I'm compiling by using the command:
mvn clean compile assembly:single
and here's my 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>com.me</groupId>
<artifactId>GankALane</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>beam-releases</id>
<url>https://maven.beam.pro/content/repositories/releases/</url>
</repository>
<repository>
<id>beam-snapshots</id>
<url>https://maven.beam.pro/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>pro.beam</groupId>
<artifactId>api</artifactId>
<version>1.10.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>pro.beam</groupId>
<artifactId>interactive</artifactId>
<version>1.5.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.me.GankALane.Main</mainClass>
</manifest>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Thanks for looking, and hopefully I can find a solution!
Your POM is wrong:
<configuration>
<archive>
...
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</archive>
</configuration>
The message gives the hint:
parameter archive: Cannot find setter, adder nor field ... for 'descriptorRefs'
See Apache Maven Assembly Plugin > Usage:
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
which also links to Apache Maven Archiver at the beginning: no <archive>/<descriptorRefs> there.

Export a jsprit maven project to an executable jar

I'm trying to create an executable .jar from a maven project using eclipse mars. The project itself can be found on https://github.com/jsprit/jsprit
So far I have successfully imported jsprit to eclipse as a maven project and I am able to run examples as Java Application.
I haven't yet been very successful at exporting it as an executable jar. Let's say I want to export SimpleExample.java. I have created the .jar but if I try to run jsprit-examples-1.6.2-SNAPSHOT-jar-with-dependencies.jar I get the following error message:
Error: Could not find or load main class jsprit.examples.SimpleExample
The pom.xml which is located under jsprit-examples is following:
<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">
<parent>
<groupId>jsprit</groupId>
<artifactId>jsprit</artifactId>
<version>1.6.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jsprit-examples</artifactId>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>enforce</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>jsprit.examples.SimpleExample</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/resources/assemblies/jar-with-dependencies.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>jsprit-snapshots</id>
<url>https://github.com/jsprit/mvn-rep/raw/master/snapshots</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jsprit-instances</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jsprit-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jsprit-analysis</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
Everything else is as same as in the github repository.
I ran 'Maven Build...' on jsprit-examples. Under 'Edit Configuration->Goals' I did 'clean package assembly:single'.
This is my first experience with a maven build so it might be a very simple problem to someone who has some experience with maven.
Thanks!
I you want to put all the dependencies in one executable jar, you need to create an uber-jar. I usually do this with the maven shade plugin.

Combine multiple jar into one (using maven)

I have a project in javafx , I have 3 dependencies I try to combine them with my principale jar using maven :
The result I got a jar (1.82mb) but when I click he dosen't launch noting appears.
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>
<dependencies>
<dependency>
<groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>fxgraphics2d</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.0.6_20</version>
</dependency>
</dependencies>
<groupId>groupId</groupId>
<artifactId>FXCalc</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
and this is some screenshot :
The problem : the jar file I got dosen't work , it dosen't want to launch , I tried using ant and gradle but I don't know how to use them.
EDIT : After trying the solution I get a jar but it still dosen't launch and I have no error.
EDIT 2 :
<?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>
<dependencies>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>fxgraphics2d</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.scilab.forge</groupId>
<artifactId>jlatexmath</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.0.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>sample.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<groupId>groupId</groupId>
<artifactId>CalculatorFX</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Add the following plugin to your pom.xml after dependencies:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>sampler.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then run the mvn package command from your base directory, where your project is. This will generate a single jar in the target folder.

Categories