I´m trying to build an executable jar with spring boot. It seems like that the jvmArguments that I've configured in the spring-boot-maven-plugin are not interpreted when running the jar on the server.
Here is the part of my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<jvmArguments>-Xmx256m</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
I am building the application with:
mvn clean package
This is how i start my application:
[tomcat#dps-8 klstest]$ ./klsprovider-1.1.0.jar --spring.config.name=application-prod
And this is where I'm hoping to see the jvmArguments:
[tomcat#dps-8 ~]$ jps -lvm | grep 33806
33806 /tmp/klstest/klsprovider-1.1.0.jar --spring.config.name=application-prod -Dsun.misc.URLClassPath.disableJarChecking=true
[tomcat#dps-8 ~]$
Why are the parameters not recognized/interpreted?
Spring Boot executable JAR is build with repackage goal which doesn't support jvmArguments option. This option is recognized by run goal, which will start the application locally during development.
spring-boot-maven-plugin run phase is intended to run your application locally within a Maven process or a Maven forked process depending on configuration. Mainly for test or debugging purpose.
If you have built jar spring-boot-maven-plugin run phase is not applied at all. You need to specify parameters separately for your prod system.
Related
I am new to Liquibase. I have already included maven plugins and liquibase to my pom.xml however when i update liquibase using mvn liquibase:update I get this error:
No plugin found for prefix 'liquibase' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo]
How can I fix this error so that when I type mvn liquibase:update it will run properly
Here are some of the dependencies that are in my pom.xml related to liquibase
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.3</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>
</plugin>
</build>
I don't think the problem is with liquibase plugin itself. It's more about maven prefixes.
Try executing: mvn org.liquibase:liquibase-maven-plugin:update
Also, check out this question.
Make sure you are in the right path when you are executing mvn liquibase:update command. And also try to run the command in a separate terminal like command prompt. In my case I'm using the terminal in IntelliJ IDEA when I had the problem but when I tried it in different terminal, it worked flawlessly.
In my case problem was in path. I was trying to start command c:\mvn.cmd liquibase:status from wrong folder.
Running from c:\projects\My_current_project\mvn liquibase:status works like a charm. c:\projects\My_current_project\ is folder, where pom.xml is.
In my case the issue was my pom is configured in such a way that the liquibase plugin is only defined under certain profiles which need to be passed in before the mvn liquibase:<your command here> gets run.
for example, if the pom.xml has a local profile and your liquibase plugin is ONLY defined under that profile, you should run something like mvn -P local liquibase:update in order for maven to pick up liquibase (since it's only defined in that profile)
I have a project with finalised version in pom files , lets say 12.3.45 .
I have built the code for this version some time ago already, all the built jars are in the local maven repo.
Then at some point I have run mvn clean, so all the target folders are being removed.
And now I want to execute some code, as quickly as possible, using mvn exec:java. Preferably without building anything, because why not? all the jars at some point were already built, and I know there were no code changes after that. How can I force maven to execute the code as fast as possible , not recompile anything, and just reuse the jars from the local repo?
Thanks.
If your artifacts are in a local or remote repository you can use them as dependencies.
You can use exec-maven-plugin's options includeProjectDependencies or includePluginDependencies to use them in java execution
https://www.mojohaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies. includeProjectDependencies option is enabled (true) by default.
You can execute exec-maven-plugin without building anything with mvn exec:java command
Instructions:
To run exec-maven-plugin you would need a main class to run. I assume you have one in your project. If you don't - you need to make a separate project with a main class.
Create a blank maven project.
In the project add exec-maven-plugin configuration. Set the mainClass
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>pack.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Include you artifacts as dependencies to the project
<dependencies>
<dependency>
<groupId>my.group</groupId>
<artifactId>myartifact</artifactId>
<version>12.3.45</version>
</dependency>
</dependencies>
Run mvn exec:java to execute com.my.package.MyMainClass main class from my.group.myartifact artifact
Edits:
includeProjectDependencies option is enabled (true) by default
I'm using GAE, I usually do Deploy with a plugin for eclipse where it gives me the option to send it a version to upload, however I want to do it by command line, the command is as follows
mvn appengine:deploy
However I want to set the "version" parameter, but I do not know how.
To solve it I added the following plugin in the pom:
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.120.v20160803</version>
<configuration>
<gcloud_directory>/usr/lib/google-cloud-sdk</gcloud_directory>
</configuration>
</plugin>
And I executed the following command:
mvn gcloud:deploy -Dgcloud.version=VERSION
Imagine a normal java maven project with a Main class that produces the artifact project-a.jar. This project has a dependency on project-b.jar.
Is there a Maven plugin that allows to run that jar by a command like that?
mvn run-plugin:run org.mygroup:project-a:3.1 <args>
The plugin would resolve the runtime dependencies (using META-INF/maven/(...)/pom.xml), install the project and its dependencies to the local maven repository (if not already there), construct the classpath and invoke
java -cp (...)/project-a-3.1.jar;(...)/project-b-2.1.jar org.mygroup.Main <args>
I know that the usual way is to build an executable (fat) jar that contains the dependencies, but that's not what I am asking for.
Actually, it is not even necesary to read the pom from the jar, because maven can download it from the repositories given the coordinates.
Why this question is different to the Maven Run Project question:
I do not want to start from having the project's source already checked out. So the usual use of the exec plugin is not applicable. The OP of the Maven Run Project question obviously assumed the presence of a source code project folder. Her purpose was testing and she accepted an answer that clearly needs a project. The wording of both questions is correct, too. There is a difference between the words "project" and "jar" and their actual meaning in their respective contexts is quite different.
You can use the appassembler-maven-plugin plugin, it creates a shell script that has the dependencies in the classpath for you. Heres an example config
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<extraJvmArguments>-Xms256m -Xmx1536m</extraJvmArguments>
<programs>
<program>
<mainClass>com.package.MyMainClass</mainClass>
<name>TestFormattingUtils</name>
</program>
</programs>
</configuration>
</plugin>
You can find the output script in .../target/appassembler/bin You can manually inspect the script and you'll see that its doing the type of command you wanted where it adds the jars to classpath via the command line. ie java -jar (...)/project-a-3.1.jar -cp (...)/project-b-2.1.jar <args>
I'm not a fan of jars-in-jar either, but I do maintain various tools with lots of dependencies. So, at one point, I decided to write an executable AppBoot jar which puts all the jars from a lib-subdirectory in a class-loader and then calls the main-method of the desired (executable) jar. This question prompted me to investigate if the exec-maven-plugin could do something similar, and it can.
The exec-maven-plugin does not require a "Java project" directory, but a pom.xml in a directory is required. The pom.xml I used is shown below, note that it can be placed in any (empty) directory and the application can be started by opening a shell/prompt in that directory and executing mvn exec:exec. Use mvn -X exec:exec to review the classpath used by the exec-maven-plugin.
<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.descartes</groupId>
<artifactId>exec-embed-demo</artifactId>
<version>1.2.2-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- Start the demo using Maven repository artifacts, execute with "mvn exec:exec" -->
<properties>
<demo.version>1.2.1.GH</demo.version>
<mainclass>com.descartes.basicjsp.embed.demo.Launch</mainclass>
<appname>${project.artifactId}</appname>
<homedir>${project.basedir}/</homedir>
</properties>
<dependencies>
<dependency>
<!-- exec-maven-plugin will get all required (runtime) jar-files from this dependency. -->
<groupId>com.descartes</groupId>
<artifactId>basic-jsp-embed-demo</artifactId>
<version>${demo.version}</version>
</dependency>
</dependencies>
<build>
<!-- The "outputDirectory" is added to the classpath by the exec-maven-plugin. -->
<!-- Add this pom's directory to the classpath instead of "./target/classes". -->
<!-- The directory should contain "logback.xml" to prevent a million lines of debug output from Tomcat. -->
<outputDirectory>${homedir}</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<!-- mvn exec:exec configuration -->
<!-- Embedded Tomcat will not stop with "ctrl-c" -->
<!-- Use http://localhost:8080/shutdown instead -->
<configuration>
<executable>java</executable>
<arguments>
<argument>-Dapp.name=${appname}</argument>
<argument>-Dapp.home.dir=${homedir}</argument>
<argument>-Dapp.conf.dir=${homedir}</argument>
<argument>-cp</argument>
<classpath/>
<argument>${mainclass}</argument>
</arguments>
</configuration>
<!-- mvn exec:java configuration -->
<!-- "ctrl-c" stops Tomcat but embedded Tomcat fails to start properly, probably a classloader issue. -->
<!--
<configuration>
<mainClass>${mainclass}</mainClass>
<systemProperties>
<systemProperty>
<key>app.name</key>
<value>${appname}</value>
</systemProperty>
<systemProperty>
<key>app.home.dir</key>
<value>${homedir}/</value>
</systemProperty>
<systemProperty>
<key>app.conf.dir</key>
<value>${homedir}/</value>
</systemProperty>
</systemProperties>
</configuration>
-->
</plugin>
</plugins>
</build>
</project>
AppBoot is part of the basic-jsp-embed project that uses embedded Tomcat and that project can be found here (to install, download the latest release, unpack the zip-file and run "mvn install" in the root directory of the multi-module project).
On a side-note: managing a jar-set is tricky, use tools like jHades to verify you will not run into trouble with multiple versions of the same class in different jar-files.
You are looking for the maven exec plugin.
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"]
would run your program
Maven can not do what you want, simply because it has no way to resolve the dependencies of project A once it has been built into a final jar.
Maven does not magically download libraries from the Internet: what makes it work are the definition of repositories inside the pom.xml. Without pom.xml, like you seem to suggest, how would it know where to download libraries from? Maven is not a downloading tool, it is a project management tool and what you have is no longer a project but a final library.
Since you have control over project A, you should really rely on Maven conventions and either build a fat jar or an assembly (with maven-assembly-plugin).
By the way, the pom.xml file located under META-INF is not guaranteed to exist, and, in fact, it is not there if you look at Spring artifacts. Take a look at Maven Archiver documentation: the presence of this pom file is controlled by the addMavenDescriptor boolean attribute. Set this attribute to false and your main artifact will not have this pom file.
I am developing a Java web application, using a multi-module maven project. The project setup is the following:
pom.xml Main maven project, that includes the following modules:
persistence: Entity classes and DAOs
business: Service definition and implementation
webapp: Apache wicket web application
The dependency hierarchy is the following: webapp depends on business, which depends on persistence.
I am also using the Jetty Maven Plugin to run the web application locally using mvn -pl webapp jetty:run inside the directory with the main pom.xml. When developing the application, When making code changes, I want the jetty server to restart and reload the modified code files automatically. This works fine when I am modifying files inside the webapp module, but does not work when I am modifying a file inside another module, such persistence or business.
The Maven Jetty Plugin is configured inside webapp/pom.xml as follows:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.2.v20140723</version>
<configuration>
<reload>automatic</reload>
<scanIntervalSeconds>1</scanIntervalSeconds>
<webApp>
<extraClasspath>../business/target/classes/;../persistence/target/classes/</extraClasspath>
</webApp>
<scanTargets>
<scanTarget>../business/target/classes</scanTarget>
<scanTarget>../persistence/target/classes</scanTarget>
</scanTargets>
</plugin>
I followed the instructions of this answer. The <scanTarget> tags work fine, since jetty gets restarted when I modify a file inside business or persistence. However, the <extraClasspath> does not work since the modified files are not loaded by jetty. The linked answer uses the <webAppConfig> tag. However, I am using the <webApp> tag as specified in the documentation of the plugin (I also tried the old <webAppConfig> tag, which lead to the same results).
My question is: How to configure the Jetty Maven Plugin for a multi-module project, such that it reloads modified files from other modules?
To force the reload anytime a submodule is changed you can use the following options
1 - Static module names and scan targets
You can define as scan targets the target directory for each module
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.plugin.version}</version>
<configuration>
<scanIntervalSeconds>${jetty.scanInterval}</scanIntervalSeconds>
<scanTargets>
<scanTarget>module-name/target/classes</scanTarget>
<scanTarget>module-name2/target/classes</scanTarget>
</scanTargets>
</configuration>
</plugin>
2 - Dinamic module names and scan targets (recommended)
This uses RegEx to find the compilation target for other modules, on the following example, we are reloading the application everytime a class is compiled on any module
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.plugin.version}</version>
<configuration>
<scanIntervalSeconds>${jetty.scanInterval}</scanIntervalSeconds>
<scanTargetPatterns>
<scanTargetPattern>
<directory>${project.basedir}</directory>
<includes>
<include>**/target/classes/**/*.class</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
</configuration>
</plugin>
Using trial and error, I found a solution. The problem is that jetty is executed using from the parent pom using
mvn -pl webapp jetty:run
The command is called from the directory of the main pom, thus jetty cannot resolve the relative paths inside the extraClasspath correctly. When executing the jetty:run goal inside the webapp directory, all modified classes are loaded correctly.
I assume the scanTargets are working correctly even when using mvn -pl webapp jetty:run, because the relative paths get resolved during the execution of the plugin (with the correct working directory). Jetty outputs the scan targets on startup:
[INFO] Added extra scan target:C:\PathToProject\business\target\classes
[INFO] Added extra scan target:C:\PathToProject\persistence\target\classes
However, the <extraClasspath>property is part of the <webApp> property, which is an instance of the org.eclipse.jetty.webapp.WebAppContext class. I assume that this instance is passed to jetty directly and that the extraClasspath property is accessed by jetty when it is already started.
The following configuration works for me
<!-- To launch embded jetty server -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.4.2.v20110526</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<webAppConfig>
<contextPath>/${project.name}</contextPath>
<extraClasspath>target/classes;../services/target/classes;../util/target/classes</extraClasspath>
</webAppConfig>
<scanTargets>
<scanTarget>target/classes</scanTarget>
<scanTarget>../services/target/classes</scanTarget>
<scanTarget>../util/target/classes</scanTarget>
</scanTargets>
</configuration>
</plugin>