How are third party tools such as this accessed within Java? - java

https://github.com/bvanalderweireldt/concurrent-unique-queue
I have tried to set up a Maven dependency within IntelliJ, but I am not sure how the contents of this repository should be built and imported into a Java project. Could someone with more experience please advise on how this is done?
Kind regards,
L

If you want to use this project in another project, you will create a dependency to this using the dependency entry mentioned on the github readme:
<dependency>
<groupId>com.hybhub</groupId>
<artifactId>concurrent-util</artifactId>
<version>0.1</version>
</dependency>
For this, you need the artifact in your local maven repository*. For this, you need to build this project or use a reference from Maven Central (Thanks #Mark Rotteveel )
Clone the project locally, you need to build it in one of the following ways
Build it from the command line: Navigate to the project's location in your shell (bash or cmd) and run mvn install
This will build the project and add the artifact (jar) to the local .m2 repository.
Import to Intellij Idea (File -> New -> From Existing Sources). Once imported, build this project from the "Maven Projects" view.
Once you have done this, you can use this in other projects using the <dependency> entries
*For production ready apps, you may want to have a common maven repository for team your like Nexus or Artifactory and use that to maintain artifacts. You would also have a build system like Jenkins.

In the link you gave it had the dependency Maven entry for that library.
<dependency>
<groupId>com.hybhub</groupId>
<artifactId>concurrent-util</artifactId>
<version>0.1</version>
</dependency>
That entry would need to be nested into you <dependencies> tag. Like the example below.
<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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.hybhub</groupId>
<artifactId>concurrent-util</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Related

Can the submodules use the reactor as parent in Maven?

Hello Stackoverflow community,
i recently started working with maven to see what is possible or better to see if its easier to have a dependency manager or including everything to your own.
Basic Informations
Repository
https://github.com/JXCoding/MavenTests
Reactor
<groupId>de.jxson.maven</groupId>
<artifactId>MavenTests</artifactId>
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>API</module>
<module>v1_18_R1</module>
</modules>
submodule API
<parent>
<artifactId>MavenTests</artifactId>
<groupId>de.jxson.maven</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>API</artifactId>
<dependencies>
...
</dependencies>
Which depends of reactor as parent
submodule v1_18_R1
<parent>
<artifactId>MavenTests</artifactId>
<groupId>de.jxson.maven</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>v1_18_R1</artifactId>
<dependencies>
<dependency>
<groupId>de.jxson.maven</groupId>
<artifactId>API</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
Which depends of API and reactor as parent
My Problem is:
When I build with mvn clean package, there is an error on my module v1_18_R1 in which a class from API is not found.
Questions:
Why a class is not found if the required dependency is already added?
Usually maven reactor is used for:
Collects all the available modules to build
Sorts the projects into the correct build order
Builds the selected projects in order
And not to be as parent of another projects. If you need to centralize libraries , versions, etc create a extra project with <packaging>pom</packaging>.
Solution
Remove the parent from your submodules
Add this to all of your submodules which requires spigot jars
<repositories>
<repository>
<id>elmakers-repo</id>
<url>https://maven.elmakers.com/repository/</url>
</repository>
</repositories>
WIth these fixes, I was able to build your git repository with mvn clean package
Spigot notes
offical git repository don't build https://hub.spigotmc.org/stash/projects/SPIGOT
Official repository don't works: https://www.spigotmc.org/wiki/spigot-maven/
Only this repository works https://maven.elmakers.com/repository/org/spigotmc/spigot/

Using Git with IntelliJ and Maven

If I wanted to use just the normal git via the command line and not the one in IntelliJ, what do I need to include in the version control so when I download it, I can get the Maven libraries without manually installing them?
Edit: There is no pom.xml file when the libraries are added to an IntelliJ project, so I was wondering what I need to include so Maven inside IntelliJ can download the libraries.
what do I need to include in the version control so when I download it, I can get the Maven libraries without manually installing them?
The pom.xml file does this:
Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.
Running mvn install will cause Maven to download your dependencies.
Intellij will automatically understand the changes in the pom files and update libraries of course you should have pom.xml file.
If its a maven based project, you definitely need a pom.xml file like below
<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.test</groupId>
<artifactId>testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testing</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Once you have a pom.xml file, you can define dependencies like the JUnit dependency defined above with the version you need and maven will automatically take care of downloading the dependency for the project. Once you add any new dependency to the pom.xml, you can run "mvn clean install" from the directory where you have the pom.xml file so that it installs the new dependency.
Hope this helps.

Add one Java program as a dependency in another program using Maven.

There are many related questions to this but I am getting confused with the answers from them and decided to ask this myself. I have a Java program and want to use it in another one. How can I add the first one as a dependency in the POM.xml file of the second program? The IDE I am using is IntelliJ version 13.
If the first java program was built with maven (it has pom.xml with groupId:artifactId:version), you can add it as a dependency into your new project:
<dependency>
<groupId>old-program-group-id</groupId>
<artifactId>old-program-artifact-id</artifactId>
<version>version-you-want-to-re-use</version>
</dependency>
If no - it would be very complicated way (in size of an article)...
Suppose this is pom.xml of project A
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.a</groupId>
<artifactId>a</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>a</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
After you compile project A through mvn install command, you can add project A into project B by using <dependency> of project A
suppose this is pom.xml of project B
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maventest</groupId>
<artifactId>mytest2</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>b</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.a</groupId>
<artifactId>a</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
See more information : How do I add a project as a dependency of another project?
The easiest way to make one a dependency of the other would be to put them together under a parent POM. Building multi-module Maven project is easy. This will get your started. Once you have created the parent pom and referenced it in the two modules, just use a standard maven <dependency/> tag in the one needing the dependency and create a new Intellij project that imports from the parent POM.xml file.
Follow this steps
Create jar file (java archive) with your project.
Execute
mvn install:install-file -Dfile=c:\your file.jar -DgroupId=your.id
-DartifactId=your name a -Dversion=1.0 -Dpackaging=jar
to include at your local repository
Add dependency to pom.xml
<dependency>
<groupId>your.id</groupId>
<artifactId>your name</artifactId>
<version>1.0</version>

How to get all of required Apache POI library using Maven?

I have installed maven and I created a project using this command:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
The result is there are 2 folder and 1 file created in my-app folder: src, target, and pom.xml.
Then I modify the pom.xml in order to get the all of required apache POI jars.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.my-app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- This is what I added -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
</dependencies>
</project>
Then I run:
mvn package
but no jars downloaded into the project folder although I got message "BUILD SUCCESS".
What you've done is correct. However, the poi jars won't download to your project folder but to your local Maven repository. This is exactly what Maven is supposed to do so that you don't have to manage many libraries/jars yourself and get all in a mess. If you do a search of your local Maven repository, you should find it there.
I also suggest you read up on how Maven uses external dependencies, this is all explained here:
http://maven.apache.org/guides/getting-started/index.html#How_do_I_use_external_dependencies
If you want to package up all of your dependent jars in to one big jar look here:
How can I create an executable JAR with dependencies using Maven?
Your project has a jar packaging type. Java not support nested jar and then maven package doesn't put any jar in your project . To do this you have to use Maven Assembly Plugin or use Spring-boot to make your uber jar

Maven: Deploy on Save of EAR in Netbeans only works when dependency project is open

The deploy on save option of my EAR project is not working and I don't understand why. Here is my structure:
myproject-ear, packaging: EAR
--->myproject-core, packaging: JAR (ejbs)
--->myproject-web, packaging: WAR (.xhtml pages, some javascript and CSS)
I'm using maven and I have the war references the JAR as a provided dependency.
The thing is I have a Nexus repository to handle my JAR versioning, I do not develop with my JAR project open. But if I close my JAR project and then deploy the application the fast deployment simply stops working on glassfish (it doesn't even generate a gfdeploy on my EAR target folder, it instead copies all files to the glassfish directory).
Here are my (simplified) pom files:
Father project:
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>myproject</name>
<modules>
<module>myproject-web</module>
<module>myproject-ear</module>
</modules>
EAR project (uses maven-ear-plugin):
<parent>
<artifactId>myproject</artifactId>
<groupId>mygroupid</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>myproject-ear</artifactId>
<packaging>ear</packaging>
<name>myproject-ear</name>
<dependencies>
<dependency>
<groupId>mygroupid</groupId>
<artifactId>myproject-core</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>mygroupid</groupId>
<artifactId>myproject-web</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
WAR project (uses maven-war-plugin):
<parent>
<artifactId>myproject</artifactId>
<groupId>mygroupid</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>myproject-web</artifactId>
<packaging>war</packaging>
<name>myproject-web</name>
<dependencies>
<dependency>
<groupId>mygroupid</groupId>
<artifactId>myproject-core</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
It may be a bug with your version of Netbeans. Try Netbeans version 7.3 and see if it works there. The issue I mentioned says it works in 7.1.2 but I'd give 7.3 a shot first. The issue was reported resolved in 7.3 and only broke again for the 7.4/8 development builds.
You are mixing lots of different magic (NetBeans, Maven, Nexus, and auto-deploy) and it's not surprising that it doesn't work exactly the way you would like. It's not clear (from an abstract tool developer's perspective) what the right thing to do is when you are trying to include in the deployment the latest version of a project under active development (that is, a project open in Netbeans) but that project is closed. Falling back to the version in the Nexus repository probably wasn't in the mind of the Netbeans developers who implemented auto-deploy.
My suggestion is to create another workspace where you do not include the JAR as a project but rather strictly treat it as a third-party library in the Maven and NetBeans configurations. Use this workspace except for when you need to work on the JAR.
Or else just leave the JAR project open.

Categories

Resources