I'm trying to use Javalin in my project and I can't seem to understand how to add the needed dependencies in order to work with Javalin without compliation errors.
The project i'm working on is not a Maven project, it is a simple Java project so it won't be downloaded automatically.
How do I add the dependencies and where?
I am using VSCode but can Switch to Intellij IDEA if needed.
Thanks.
At the risk of pointing you in a direction you may not want to go in... Use a dependency manager (Maven, Gradle, Ivy, or similar). Simple Java projects can be dependency-managed projects, too!
A basic Javalin project includes dozens of dependencies - and dependencies of those dependencies... You will probably have an unpleasant time attempting to handle them all manually, one-by-one.
If you use the Javalin bundle, that will take care of all of this for you.
To give you a sense of what I mean:
If you do decide to use a dependency manager, then your follow-up questions are well covered elsewhere. Or you can ask a follow-up, based on any problems you may encounter.
Update
Yeah but were doing it in a school project and were already half way through the project and now I need to add a Web Client and we don't want to change things all through the project, there's gotta be a way to add those dependencies without creating a new Maven project for it.
You can install Maven and run a command to download all the JARs to a directory.
This is (in my opinion) more work than just using Maven already built into all mainstream IDEs, but here are the steps:
Note: My set-up assumes Windows. You can adjust as needed for Linux or a different OS.
Download Maven - see here.
I downloaded the binary zip archive.
Set up Maven - see here.
Be sure to pay particular attention to the instructions regarding setting the JAVA_HOME environment variable pointing to your JDK installation or having the java executable on your PATH.
I installed my Maven here:
C:\maven\apache-maven-3.8.5
I tested it in a shell using the mvn -v command:
C:\maven\apache-maven-3.8.5\bin\mvn -v
Create a pom.xml file. Maven uses this as its instructions for what to download (and to what location).
In my case I created the POM here:
C:\maven\demo\pom.xml
Its contents are:
<?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>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<groupId>org.andrewjames</groupId>
<artifactId>my-Javalin-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin-bundle</artifactId>
<version>4.5.0</version>
</dependency>
</dependencies>
<build>
<finalName>my-Javalin-demo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<silent>true</silent>
<outputDirectory>C:/maven/demo</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<name>my-Javalin-demo</name>
</project>
The maven.compiler sections assume I have Java 17 available. You may need to adjust to match your Java version.
The dependencies section is where the javalin-bundle is defined.
The execution section is the directive which causes all dependency JARs to be downloaded to the Maven local repository, and then copied to a new directory.
In my case the new directory will be created here:
C:\maven\demo\target\dependency
Open a CMD shell and cd to C:\maven\demo
At the command line, run the following command:
C:\maven\apache-maven-3.8.5\bin\mvn dependency:copy-dependencies
After that has completed, you will see approx. 100 JAR files in the C:\maven\demo\target\dependency directory.
Related
Having the following situation:
STS
Java
Maven
Machine One
workspace-01
The Java app with Maven is based for a single module.
Through either mvn package or mvn install for the app the respective jar is generated. Of course both are the same, but we have the solid confidence that the most valid or stable is the available through the local repository.
Now through mvn install the jar is installed in the local repository and using a script and referring the local repository is possible find and copy the master-project.jar file in other Machine and then is installed manually. It for simplicity purposes.
The point is: that for the other project that dependency can be used in peace how
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1.SNAPSHOT</version>
</dependency>
Until here all work fine
workspace-02
The app grew up and in other workspace the app was migrated to work with multi-modules
Lets assume the following structure for simplicity
master-project (<packaging>pom</packaging>)
alpha (<packaging>jar</packaging>)
beta (<packaging>jar</packaging>)
numbers (<packaging>pom</packaging>)
one (<packaging>jar</packaging>)
two (<packaging>jar</packaging>)
countries (<packaging>pom</packaging>)
Europe (<packaging>pom</packaging>)
France (<packaging>jar</packaging>)
Italy (<packaging>jar</packaging>)
AmericaLatina (<packaging>pom</packaging>)
Peru (<packaging>jar</packaging>)
Argentina (<packaging>jar</packaging>)
uber-jar (<packaging>jar</packaging>) <--- it has a special purpose
I am able to compile all these modules. Therefore build success
Now the uber-jar module has the unique purpose to define all the dependencies based on <packaging>jar</packaging> type to generate the uber jar through the maven shade plugin. The configuration is as follows:
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>uber-jar</artifactId>
<name>uber-jar</name>
<parent>
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>alpha</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>beta</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
....
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>etc</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.plugin.version}</version>
<executions>
<execution>
<id>create-fat-jar</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>master-project-0.0.1-SNAPSHOT</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
With the current configuration shown above if mvn package is executed in the uber-jar's target directory appears the master-project-0.0.1-SNAPSHOT.jar and uber-jar-0.0.1-SNAPSHOT.jar files. Until here, about the former, it is the same behaviour than the one module approach/enviroment.
If mvn install is executed, I can see in the local repository that each module is represented through a directory, where for each <packaging>jar</packaging> type it contains its respective jar file. Even I go the uber-jar's directory in the local repository it appears its own jar, just that.
The point is that the script now fails because does not exist the master-project-0.0.1-SNAPSHOT.jar in the local repository anymore. Of course I can work around to the one generated in the uber-jar's target directory. The other approach is use the same set of dependencies in the uber-jar module in the other project, but it is verbose.
Observation: that project should be not aware that the dependency comes from either single-module or multi-module project.
Machine Two
The following must remain in peace in the other project
<dependency>
<groupId>com.manuel.jordan</groupId>
<artifactId>master-project</artifactId>
<version>0.0.1.SNAPSHOT</version>
</dependency>
Request: if is possible, How to edit the Maven Shade Plugin configuration shown above. It with the purpose to include the target's jar into the local repository
The unique solution is install manually the master-project-0.0.1-SNAPSHOT.jar file available in the uber-jar's target directory. But being curious if my request can be accomplished or not.
As you have probably noticed yourself, <finalName> only changes the name of the artifact in target. To change the name in the local repository, you need to change the Maven coordinates in the POM.
More importantly, copying from one local repository to another is not a recommended approach. Instead, the standard approach is to use a Nexus or Artifactory server.
I am having a group of external jars(in hundreds) which I have added in the build path of my project to make it work.
But while packaging it is failing as these jar's are not available to maven.
I have gone though many articles and all the solutions(like adding the jar at system path) are for a single jar only.
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>
Is there any way we can add the group of jars(folder) to the packaging on the project? or any other solution by which my project can build successfully?
can we create a single jar containing all my jars inside and then use the system scope of maven
I have tried creating jar by jar -cvf my_jar.jar * and placed this jar in the system scope. But it does not worked for me.
My solution : Maven pluggin addjar let us add all jar at a place(projectdirectory/lib in this case).
this enables you to add these jar's in the final package(jar in my case) when you maven build, but to run locally you have to add those jar files directly in the classpath.
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.5</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${basedir}/lib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Now create a shade jar using mvn clean install shade:shade
The bad news: For a proper Maven project, you need to add each and every artifact you use as <dependency> in your POM.
The good news: I very much doubt that these 100 jars are all
- directly used in your source code
- not available in a public Maven repository like MavenCentral
So the better strategy would be to figure out what you really need and find that in MavenCentral. Then Maven finds all the transitive dependencies for you. So if you really need 10 of the jars and all other jars are just dependencies of your dependencies, just add these 10 ones (from MavenCentral) and you are done.
my team and I have a maven project using jdk1.8 (not open jdk) and javaFX for our UI. It's working well and there still a lot of work to do and we'd like to use the pipelines from Bitbucket to make continuous integration on the go.
I tried to use a simple .yml file :
image: maven:3.3.9-jdk-8
pipelines:
default:
- step:
caches:
- maven
script: # Modify the commands below to build your repository.
- mvn -B verify # -B batch mode makes Maven less verbose
But there is my problem, We are developping with IntelliJ and jdk1.8 where javaFX is standard and automatically included in the project. But the pipelines tries to use openjdk1.8, and can't find the javaFX classes (especially the jfxrt.jar file).
If I do nothing and try as it is, the Maven error is :
[ERROR] /opt/atlassian/pipelines/agent/build/src/main/java/JFX/MainJFX.java:[3,26] package javafx.application does not exist
[ERROR] /opt/atlassian/pipelines/agent/build/src/main/java/JFX/MainJFX.java:[4,19] package javafx.fxml does not exist
[ERROR] /opt/atlassian/pipelines/agent/build/src/main/java/JFX/MainJFX.java:[5,20] package javafx.scene does not exist
[ERROR] /opt/atlassian/pipelines/agent/build/src/main/java/JFX/MainJFX.java:[6,20] package javafx.scene does not exist
[ERROR] /opt/atlassian/pipelines/agent/build/src/main/java/JFX/MainJFX.java:[7,20] package javafx.stage does not exist
I tried to include the jfxrt.jar file to maven to my pom.xml as a system dependency like that :
<dependency>
<groupId>javafx</groupId>
<artifactId>jfxrt</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/lib/ext/jfxrt.jar</systemPath>
</dependency>
But then Maven warns me he does not find the jfxrt.jar file :
[WARNING] 'dependencies.dependency.systemPath' for javafx:jfxrt:jar refers to a non-existing file /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/jfxrt.jar # line 34, column 25
Mainly because he is using openjdk and not jdk.
I also tried to put the jfxrt.jar file inside the repository and make a dependency to it, it works but Maven warns me it's not proper to do it like that :
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${basedir}/dependency/jfxrt.jar</systemPath>
</dependency>
and the warning :
[WARNING] 'dependencies.dependency.systemPath' for com.oracle:javafx:jar should not point at files within the project directory, ${basedir}/dependency/jfxrt.jar will be unresolvable by dependent projects # line 34, column 25[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
My question is how to either force Maven to use JDK and not OpenJDK, or how to make a nice dependency to javaFX so Maven can verify our project everytime we push and not warn us.
Thank you all for your future responses.
Due to Oracle's licensing of Java, Docker Hub no longer hosts official images containing the Oracle JDK. All the official images, including the Maven one you're using, are now based on OpenJDK instead.
To use Oracle Java, you'll need to build your own Docker image as the Pipelines build environment (relatively simple), or find a trusted source for a Docker image with the JDK in it. (It's pretty easy to find these via Google search, because this is a common problem.)
Frist: JavaFX ist part of the OpenJDK. This setup works for me since the open beta of piplines:
the yaml file
image: maven:3.3.3
pipelines:
default:
- step:
script: # Modify the commands below to build your repository.
- mvn clean install
and here the 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>my.group.id</groupId>
<artifactId>my.artifact.id</artifactId>
<name>my.app.name</name>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- goal is 'mvn jfx:jar' -->
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.7.0</version>
<configuration>
<mainClass>package.to.my.main.class</mainClass>
</configuration>
<executions>
<execution>
<id>create-jfxjar</id>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I've inherited a Maven project. I'm just using it as a build tool and I'd like to disturb things as little as possible. I have to make a slight addition to one of the Java files and that addition requires that I include a new jar on the build path. How do I say: here a jar, just use it. It doesn't have to be versioned or depended or downloaded or anything, just use it. Any help would be appreciated.
EDIT: I found this, which actually works (!). If someone who knows about such things could read this answer and if it seems reasonably correct, please close this question as a dup.
EDIT: Nope, I misinterpreted my results. It doesn't seem to work.
By far the best way to manage your dependencies with maven is to get them from a repository, but four total options spring to mind, in order from most desirable to least:
If the jar is a common third-party library, you'll almost certainly find it in some repository somewhere. You just have to add a <dependency> element and possibly a <repository> as well so it knows where to get the dependency from.
A home-grown jar not available in any repo should be deployed to a local repository, like Nexus, which is available to your whole team/company. Then add the dependency to your project just like in option 1. This way it only has to be dealt with once, and everyone else can get the jar via the normal Maven mechanism.
To only deal with the problem locally and not give any reusability of the artifact, you can install it into your local repo (meaning your local cache at ~/.m2/repository) using the install:install-file goal.
Finally, and least desirably, you can use a system-scoped dependency. This means you have the jar file available somewhere in your file system, set the <scope> element of your <dependency> to the value "system", and add a <systemPath> element that contains the full path to the jar in question.
Edit: Since option 4 seems right for you, just put the jar into your project and commit it to your version control. Then, assuming the jar is at lib/foo.jar in your project, add this to your POM's dependencies:
<dependency>
<groupId>some-group</groupId>
<artifactId>some-artifact</artifactId>
<version>1.2.3.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/foo.jar</systemPath>
</dependency>
That's all from memory, but it sounds right.
Here are some related answers:
Maven: keeping dependent jars in project version control
I would not recommend using install:install-file from a POM - if it's a once off requirement you're better using that from the command line and documenting it as a preparation step. However, making the build self-contained or providing a repository with the required artifacts are certainly better options.
Here is how to proceed. Create a separate maven project inspired from the following 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">
<modelVersion>4.0.0</modelVersion>
<groupId>net.dwst</groupId>
<artifactId>MavenMissingJars</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Maven Missing Jars</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>dProguard-4.6</id>
<phase>generate-sources</phase>
<goals>
<goal>install-file</goal>
</goals>
<inherited>false</inherited>
<configuration>
<file>toinstall/4.6/proguard.jar</file>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard</artifactId>
<version>4.6</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Assuming there is a /toinstall/4.6/ directory relative to your pom.xml and that a jar called proguard.jar is in there, calling this plugin will copy the jar from your local directory to your maven local repository.
This has to be executed once, that's why it is preferable to have a separate small maven project for injecting missing jars.
Then, add a dependency in your project using the coordinates (artifactid, version and packaging) you have defined in the above pom.xml.
What is the 'least work' approach to distributing a Clojure application? And is this different from the 'best' approach?
For example here is a trivial 'application' that I want to be able to send to someone:
(doto (javax.swing.JFrame. "Hello World")
(.add (javax.swing.JLabel. "Clojure Distributable"))
(.pack)
(.show))
I imagine it makes a big difference to the answer whether they have Java installed already or not.
For Mac Os x, java is installed by default and apple provides JarBundler that allows you to turn jars in to native os x applications. OS X applications don't use installers.
For Windows Launch4j is a good choice it will wrap your jar into .exe with an icon also it will check if java is installed if not download it. For a windows installer i recommend NSIS (winamp's installer).
For Linux, jar file + bash script.
Compile it to byte code and then use Java Web Start (or what every Java installer floats your boat)?
For people without Java installed - Some installed can detect this and fire off the JRE installer. The web JWS does this is some Javascript on the page with the link to the JNLP file. This then will switch the link out for a JRE install link if Java is not detected.
The easiest way would be to use Maven, here's what you'll need to do:
Get a copy of Maven 2 installed.
get a copy of the Clojure Maven Plugin, go to its folder and run mvn install
Install clojure.jar into your maven repository by running the following command:
mvn install:install-file -DgroupId=org.clojure -DartifactId=clojure -Dversion=1.1.0-alpha-SNAPSHOT -Dpackaging=jar
-Dfile=clojure.jar
Now you'll need to create a pom.xml which will tell maven how to build your project
<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>org.clojure</groupId>
<artifactId>hello-world</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>com.theoryinpractise</groupId>
<artifactId>clojure-maven-plugin</artifactId>
<version>1.1-SNAPSHOT</version>
<configuration>
<sourceDirectories>
<sourceDirectory>src</sourceDirectory>
</sourceDirectories>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>clojure</artifactId>
<version>1.1.0-alpha-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
So now, say you have a hello.clj that look like:
(ns clojure.examples.hello
(:gen-class))
(defn -main[args]
(doto (javax.swing.JFrame. "Hello World")
(.add (javax.swing.JLabel. "Clojure Distributable"))
(.pack)
(.show)))
your project structure should look like:
project/pom.xml
project/src/clojure/examples/hello.clj
if you go to the project folder and run mvn install, it should create project/target/hello-world-1.0.jar which will have a main method, you should be able to run it with
java -cp hello-world-1.0.jar:clojure.jar clojure.examples.hello
You might also want to look into One-Jar project, which would let you bundle both your application and the clojure library in the same jar.
Consider Excelsior JET, if you need totally stand-alone app.
Consider some jar-to-exe wrappers, like launch4j.
These advices are for Java platform generally, rather then specifically for Clojure...