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>
Related
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.
I have a java azure function that was running package azure-functions-maven-plugin version 1.3, trying to upgrade the package to anything 1.4 or greater when I try to package the function I get the following error:
Failed to execute goal com.microsoft.azure:azure-functions-maven-plugin:1.12.0:package (package-functions) on project azure-functions-archetype: com.google.gson.stream.MalformedJsonException: Expected name at line 9 column 4 path $.extensions.http
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.microsoft.azure</groupId>
<artifactId>azure-functions-archetype</artifactId>
<version>1.38</version>
<packaging>jar</packaging>
<dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre8</version>
</dependency>
</dependency>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>1.12.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<configuration>
<resourceGroup>java-functions-group</resourceGroup>
<appName>${functionAppName}</appName>
<region>${functionAppRegion}</region>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~3</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
```
I m unable to find a solution online on what is causing this error, hoping someone out there has an idea
Failed to execute goal com.microsoft.azure:azure-functions-maven-plugin:1.12.0:package (package-functions) on project azure-functions-archetype: com.google.gson.stream.MalformedJsonException:
It seems to be the issue in pom.xml code like either the tags are misspelled or the azure functions maven plugin code is missing or written wrong.
There is a similar issue here resolved by specifying the runtime and correcting the misspelt tags/code.
Also, Please make changes in your pom.xml file by comparing with my pom.xml as I see in the given pom.xml is missing the azure.functions.maven.plugin.version, runtime OS property etc. (https://i.imgur.com/uVkrUfU.png, https://i.imgur.com/ccu4BrW.png).
Reference: pom.xml code
I tried to run the Azure Functions Project (Java Stack) in VS Code and it come up with many issues regarding to loading the dependencies, Java Compiler Packages etc. and finally running the function successfully by following the below steps:
Maven version 3.8.4
Azure Functions Maven Plugin 1.15
Process 1:
Created the Azure Java Functions Project through VS Code and run the function successfully:
Process 2:
Created azure functions project using maven-archetype-quickstart template and is working good.
Run this command in your VS Code terminal:
mvn archetype:generate -DgroupId=com.microsoft.azure -DartifactId=azure-functions-archetype -DinteractiveMode=false
Since archetypes are templates and they intend to reflect current best practices, they can evolve in time, thus they have their own versions. Maven will ask you which version of the archetype you want to use. By default, maven chooses latest version for you. so if you agree to use the latest version of an archetype, just press Enter at this step;
Every maven project (and module) has its groupId, artifactId and version. Maven will then ask these to you in three steps. groupId: This is generally unique amongst an organization or a project. artifactId: The artifactId is generally the name that the project is known by. version: This is the last piece of the naming puzzle.(read more)
Finally, maven will ask you the package structure for your code. A best practice is to create your folder structure that reflects the groupId, thus Maven sets this as default but you are free to change this.
After entering these information, Maven will show you all the information you entered and ask you to verify project creation. If you press Y and then enter, voila your project is created with the artifact and settings you chose.
You can also read maven-archetype-plugin's usage site.
To Update the pom.xml versions, please run the commands (in your VS Code Project terminal) available in this Maven official site.
I'm creating a common library that will be used accross several Spring application. I've extracted the common classes I want to share between these application into a new Spring application named "Common". I've used mvn install on this application to put it in my local maven repository. On my main project, I've added the maven dependency, with the same version, in the pom.xml.
Now, if I try to launch mvn compile on my main project, I got this error :
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project recruitment: Compilation failure: Compilation failure:
[ERROR] /D:/[...]/application/src/main/java/com/example/application/service/Service.java:[8,37] package com.example.common.exception does not exist
Indicating that my common library is not present and thus my application cannot find package coming from it.
If I try to launch my Spring application from the main (using Spring Boot), it launchs perfectly and behaves like before.
I'm using IntelliJ on Windows, maven is bundled with IntelliJ with the default installation. If I check in my local maven repository I can find the common library jar. IntelliJ does not show any error on my imports, it recognizes my library.
There seem to be some conflict between IntelliJ, which read the pom.xml to find libraries imported for my application, and maven that use the same pom.xml to compile my code.
I used mvn compile -X to have more information and the funny part is that maven is using the common library jar from the local repository :
[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG] D:\[...]\application\target\classes
[DEBUG] C:\[...]\.m2\repository\com\example\common\0.0.1\common-0.0.1.jar
Do you have any idea why I can't compile my project with maven while
I can launch it as a Spring application ?
Here is my dependency in my pom.xml :
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
<scope>compile</scope>
</dependency>
And the common library definition :
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
<name>common</name>
<description>Common project with generic technical modules</description>
Okay, that's embarassing... I just found the solution ! It has been two days looking for a solution to this tricky situation and I just found what was missing, or rather what was too much.
This bad boy :
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
The Spring boot maven plugin is what makes my Spring boot application launchable from maven, and it seems that it affects the way the common jar library is made, or the way it is imported.
I don't really understand why exactly it affects my import, but I deleted the plugin above in my common library pom.xml, I reinstalled in my local repository and I launch mvn install again on my main application... And it now works !
I think that launching via Spring boot used a little bit of Spring boot magic to make it works that the maven way lacks.
If someone understands it better and can explain it, that would be great and the accepted answer I think.
The reason for that is that application classes are packaged in BOOT-INF/classes so that the dependent module cannot load a repackaged jar’s classes
Proper config will look like:
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.1.RELEASE</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Taken from
https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/#repackage-examples
I have a very simple test project in Intellij IDEA in which I try to mix Java 9 and Groovy code. Additionally, this project has Maven Support, i.e. it is organized according to a Maven archetype and has a POM.
Now I have two classes in the default package calling each other (though not cyclically as compilation for that fails) and all is working well, i.e. Build > Build Project and Run > Run 'Main' are working.
However, this compilation seems to be independent of Maven configuration. There is no Groovy support in the POM for one. If I just compile from the command line with mvn compile compilation fails as the linker cannot find any object that would result from Groovy compilation.
The POM just contains configuration for the maven-compiler-plugin and I added that by hand with source and target nodes under configuration set to 1.9 to have IDEA compile from/to Java 9 instead of from/to Java 5. So there is some interaction between what is in the POM and what IDEA does when I select Rebuild Project.
What is correct way to configure IDEA and/or configure the POM so that compilation succeeds both in IDEA and on the command line. And if anyone knows, what are the interactions between what's in the POM and IDEA?
Do I have to configure the Groovy Eclipse Maven plugin in the POM? (I will try to do that)
This is what I have working for Java 8 and Eclipse, for a project with both Java and Groovy code. I know I found the basis for this via Google at one point but did not save the URL, alas. Not sure if it will work with Java 9 and IntelliJ but worth a shot?
<properties>
<groovy.eclipse.compiler.plugin.version>2.9.1-01</groovy.eclipse.compiler.plugin.version>
</properties>
....
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- 2.8.0-01 and later require maven-compiler-plugin 3.1 or higher -->
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<!-- set verbose to be true if you want lots of uninteresting messages -->
<!-- <verbose>true</verbose> -->
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy.eclipse.compiler.plugin.version}</version>
</dependency>
<!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.3.7-01</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy.eclipse.compiler.plugin.version}</version>
<extensions>true</extensions> <!-- required to get plugin to compile tests when no src/main/java dir exists -->
</plugin>
E.g. if I make the BukkitApi jar a dependency for a maven project with depenecy scope set to provided,compile,system, runtime or test
In which scopes will the bukkitAPI be included in the compiled output?
Short version: By default, maven output (in the default target directory) does not include anything except the compiled code for the current project/module. That is, nothing from dependencies.
Long(er) version: with default jar packaging and no custom phase configuration. here is how maven behaves on a java project:
the compile phase : the .java files in the src/main/java/ directory get compiled to .classes files in the target directory. Dependencies for the compile scope get downloaded to your local repository.
the package phase : same as 1, plus you'll get a jar file in the target directory
the install phase : same as 2 you'll get a jar file in your local repository.
So, .jar files from dependencies are not included in anything by default !
So, how do I include dependencies in my "output", and what does thoses scopes mean ?
Now, using, for exemple, the assembly plugin to include dependencies in the output of the package phase (see Including dependencies in a jar with Maven), you'll normally get this default behavior:
provided : not included
compile (default) : included
system : not included
runtime : included
test : not included
Checkout this link for reference.
EDIT: Just try out this pom with the different scope values on guice, and you'll see that dependencies are included in the fake-1.0-SNAPSHOT-jar-with-dependencies.jar when scope is compile and runtime (this example does not need any source files)
<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.linagora</groupId>
<artifactId>fake</artifactId>
<version>1.0-SNAPSHOT</version>
<name>fake</name>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>2.0</version>
<scope>compile</scope>
<!-- system scope needs 'systemPath' attribute as well
<systemPath>/path/to/guice/guice-3.0.jar</systemPath>
<scope>system</scope>
-->
<!-- <scope>runtime</scope> -->
<!-- <scope>test</scope> -->
<!-- <scope>provided</scope> -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
That's not how Maven works. The dependencies just specify the classpath (for compilation, run time, testing). But the dependencies are not included in the output by default. You will have to ship all the dependency jars (at least the ones with scope compile and runtime).
Have a look at the dependency plugin. It provides goals to copy the dependencies.
To create a bundle for shipment, have a look at the assembly plugin (e.g. to create a zip file). It even provides a way to create an all-in-one jar, if that is what you're after.