I am new to java and maven. I am learning and trying to make a system to help me build up JSON data by GOOGLE's gson and maven. This is my using command order:
mvn -f my-app\pom xml clean compile
mvn -f my-app\pom xml install
cd my-app\target
java -jar my-app-1.0.jar
After I run it it show me this error.
in GSONExample.java Line 13:
Gson gson = new Gson();
The following is my using software/IDE version.
Source Editor: VS Code
JDK: JDK-19
apache-maven: 3.8.6
Gson: 2.10 (https://github.com/google/gson/releases)
I have already gone through other websites and StackOverflow to find solutions.
GSON is not being imported into the maven project Changed scope role and still crash
error even though it is defined in my classpath /WEB-INF/lib can't find in my situation
Now below a part of my pom.xml
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>
I also had try to import it(Gson) to local and try to fix it out.
mvn install:install-file -Dfile=C:\gson-2.10.jar -DgroupId=com.myself.gson -DartifactId=example-gson -Dversion=2.10 -f my-app\pom.xml
After I use the command, I changed pom.xml as follow. It can compile and install. But still show me "NoClassDefFound"
<dependency>
<groupId>com.myself.gson</groupId>
<artifactId>example-gson</artifactId>
<version>2.10</version>
</dependency>
May anyone provide any solution to this? Thanks in advance.
I had tried
changed scope to provided in pom.xml
put Gson in src\main\resources\lib and change pom.xml
Local gson-2.10.jar and change pom.xml
put gson-2.10.jar in C:\Program Files\apache-maven-3.8.6\lib
The problem is this command:
java -jar my-app-1.0.jar
Most likely your current Maven pom.xml creates a JAR file which contains only the classes of your project, but not the classes of dependencies, such as Gson. This is also not specific to Gson but applies to any dependencies you are trying to use. You can either:
use java -cp and specify the path to your JAR as well as the paths to all the JARs of dependencies you are using, for example java -cp gson-2.10.jar;my-app-1.0.jar com.mycompany.app.GSONExample
configure Maven to build a "JAR with dependencies", see this question whose answers describe multiple ways of how this can be achieved
I am working with Bndtools in Eclipse. I am trying to add Apache Thrift as dependency to a project. My build.bnd has this code for adding the Maven Central as well as our Nexus repository:
-plugin.5.Nexus = \
aQute.bnd.repository.maven.pom.provider.BndPomRepository; \
releaseUrls=https://<NEXUS_URL>:8081/content/groups/public/ ; \
snapshotUrls=https://<NEXUS_URL>:8081/content/groups/public/ ; \
index=${.}/release.maven; \
pom = ${workspace}/cnf/maven-nexus.xml; \
name="Nexus"
-plugin.6.MavenCentral: \
aQute.bnd.repository.maven.pom.provider.BndPomRepository; \
releaseUrls = https://repo1.maven.org/maven2/; \
pom = ${workspace}/cnf/maven-central.xml; \
name = Maven Central
I tried to add the dependency to both xml files like this:
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
Other dependencies like Gson work just fine. The bnd.bnd file of the project where I want to use Thrift only has this content:
-buildpath: \
org.apache.thrift
I also tried org.apache.libthrift, but I found out that this should be the correct OSGi bundle name. I always get
Cannot find /error/org.apache.thrift;version=0 Not found
Am I missing something here? I am new to OSGi and so I might miss something really obvious. I am also not sure if my configurations of the repositories are correct. Still, other dependencies work just fine. Thanks for any advice!
EDIT:
Maybe as additional information: In Eclipse the build.bnd file is shown empty in the GUI view. Does this mean, that my project is somehow corrupted or is this normal?
EDIT2: For testing (didn't help) I also added the Maven Central using the MavenBndRepository Plugin additionally to the BndPomRepository:
-plugin.7.MavenCentralAlternative = \
aQute.bnd.repository.maven.provider.MavenBndRepository; \
releaseUrl=https://repo.maven.apache.org/maven2/; \
index=${.}/central.maven; \
name="Maven Central Alternative"
EDIT3: The problem has to be Thrift related. All other packages work just fine, only Thrift is not found. My current workaround is to add the Thrift jar to the project manually and reference its dependencies as OSGi bundle just like I tried to add Thrift in the first place. Obviously I get now this warning:
Bndtools: The .classpath contains a library that will not be available
during continuous integration
Eclipse Neon, Vaadin version 7.3.6. Java 1.8.
I want to show QR code on page , created by Vaadin. So I add dependency in pom.xml
<dependency>
<groupId>org.vaadin.addons</groupId>
<artifactId>qrcode</artifactId>
<version>2.0.1</version>
</dependency>
In my code in Vaadin I use this:
QRCode code = new QRCode();
code.setValue("The quick brown fox jumps over the lazy dog");
code.setWidth("400px");
code.setHeight("400px");
addComponent(code);
It's compile and run success. But when I try to load html page with QR code I get the next error:
Widgetset 'com.vaadin.DefaultWidgetSet' does not contain implementation for fi.jasoft.qrcode.QRCode.
Check its component connector's #Connect mapping, widgetsets GWT module description file and re-compile your widgetset.
In case you have downloaded a vaadin add-on package, you might want to refer to add-on instructions.
What is wrong? Thanks.
You need to re-compile the widgetset for your application. Actually there is a maven plugin for that. You can find the detailed instructions how to create&comiple custom wigetset here:
https://vaadin.com/directory/help/using-vaadin-add-ons/maven
It's a bit tricky to set everything up, but once it's done, it's only about running
mvn vaadin:update-widgetset install
You'll have to add some plugins to the pom.xml in order to make this work, the best way how to find out how to add the plugin is to generate the official archetype for the Vaadin application and copy&paste the plugin info from there:
mvn archetype:generate \
-DarchetypeGroupId=com.vaadin \
-DarchetypeArtifactId=vaadin-archetype-clean \
-DarchetypeVersion=LATEST \
-DgroupId=your.company \
-DartifactId=project-name \
-Dversion=1.0 \
-Dpackaging=war
Note that if you are using spring-boot then there'll be no web.xml where to define the widgetset, but you can actually follow info from this forum:
https://vaadin.com/forum#!/thread/9587133
Is there any way to input arguments when launching spring-boot application (mvn spring-boot:run) from commandline and then get them in main()?
Looking at the source code of the spring-boot-maven-plugin I found that you need to do:
mvn spring-boot:run -Drun.arguments="arg1,arg2"
Another way to get more information about what options the run goal of the spring-boot plugin supports is to execute the following command:
mvn help:describe -Dcmd=spring-boot:run -Ddetail
For Spring Boot 2.x, the source is here and you now need to use -Dspring-boot.run.arguments="args1,args2"
(edit from april 2021)
For Spring Boot 2.2+, you now need to use -Dspring-boot.run.arguments="args1 args2"
If you are using Gradle and you want to be able to pass command line arguments to the Gradle bootRun task, you first need to configure, for example like so:
bootRun {
if ( project.hasProperty('args') ) {
args project.args.split('\\s+')
}
}
and run the task using gradle bootRun -Pargs="arg1 arg2"
When passing multiple arguments using -Drun.arguments, if the argument in turn has 'comma-separated' values, then only the first value of each argument is used. To avoid this repeat the argument as many times as the number of values.
This is more of a workaround. Not sure if there is an alternative unless the delimiter is different - like '|'.
E.g Issue:
mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,dev"
Picks only 'test' profile for the above command.
Workaround:
mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,--spring.profiles.active=dev"
Picks both 'dev' & 'test' profiles for the above command.
Be aware : The way of passing arguments depends on the spring-boot major.minor version.
TLDR
For Spring Boot 1:
mvn spring-boot:run -Drun.arguments="argOne,argTwo"
For Spring Boot 2.0 and 2.1:
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
(edit from april 2021)
For Spring boot 2.2 and later:
mvn spring-boot:run -Dspring-boot.run.arguments="argOne argTwo"
spring-boot-maven-plugin version and the the Spring Boot version you use has to be aligned.
According to the Spring Boot major version used (1 or 2), the spring-boot-maven-plugin in the 1 or the 2 version should indeed be used.
If your pom.xml inherits from the spring-boot-starter-parent :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>ONE_OR_TWO_VERSION</version>
</parent>
In your pom, the version of the plugin used should not even be specified as this plugin dependency is inherited :
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
...
</configuration>
</plugin>
</plugins>
In case of your pom.xml not inheriting from spring-boot-starter-parent, don't forget to align the version of spring-boot-maven-plugin with the exact version of spring boot you want to use.
Passing arguments in command line with spring-boot-maven-plugin:1.X.X
For one argument :
mvn spring-boot:run -Drun.arguments="argOne"
for multiple :
mvn spring-boot:run -Drun.arguments="argOne,argTwo"
The maven plugin page documents it :
Name Type Since Description
arguments | String[] | 1.0 | Arguments that should be passed
to the application. On command line use
commas to separate multiple arguments.
User property is: run.arguments.
Passing arguments in command line with spring-boot-maven-plugin:2.X.X
For one argument :
mvn spring-boot:run -Dspring-boot.run.arguments="argOne"
for multiple :
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
I didn't find the plugin documentation for the 2.X.X version that refers to that.
But the org.springframework.boot.maven.AbstractRunMojo class of the spring-boot-maven-plugin:2.0.0.M3 plugin refers to this user property:
public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo
...
#Parameter(property="spring-boot.run.arguments")
private String[] arguments;
...
protected RunArguments resolveApplicationArguments(){
RunArguments runArguments = new RunArguments(this.arguments);
addActiveProfileArgument(runArguments);
return runArguments;
}
...
}
Hint : as you pass more than one argument, whitespaces between commas are considered.
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
will be interpreted as ["argOne", "argTwo"]
But this :
mvn spring-boot:run -Dspring-boot.run.arguments="argOne, argTwo"
will be interpreted as ["argOne", " argTwo"]
(edit from march 2021)
Whitespaces are now used as separator for multiple-arguments commands, see the relevant issue.
As I checked today, the correct usage for Spring Boot 2.2.5 is:
mvn spring-boot:run -Dspring-boot.run.arguments="--arg1=value --arg2=value"
Because help says:
commandlineArguments
User property: spring-boot.run.arguments
Arguments from the command line that should be passed to the application.
Use spaces to separate multiple arguments and make sure to wrap multiple
values between quotes. When specified, takes precedence over arguments.
Spring Boot 1 as 2 provide a way to pass multiple profiles as argument and avoid the issue related to the comma used both as separator between the args and the values passed as active profile.
So instead of writing :
mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=test,--spring.profiles.active=dev
use the Spring Boot Maven profiles property that is a convenience shortcut of spring.profiles.active such as the followings :
The Maven user property is different according to the Spring Boot version.
For Spring Boot 1.4+, that is run.profiles :
mvn spring-boot:run -Drun.profiles=dev,test
For Spring Boot 2, that is spring-boot.run.profiles :
mvn spring-boot:run -Dspring-boot.run.profiles=dev,test
From the plugin documentation :
profiles:
The spring profiles to activate. Convenience shortcut of specifying
the 'spring.profiles.active' argument. On command line use commas to
separate multiple profiles.
Type: java.lang.String[]
Since: 1.3
Required: No
User Property: spring-boot.run.profiles
I'm using spring.boot 2.4.2 and i separated the arguments with withe-space and put the values between double quotes.
mvn spring-boot:run -Dspring-boot.run.arguments="--param1=value2 --param2=value2"
And if you're using Eclipse...
| Parameter Name | Value |
| run.arguments | "--name=Adam" |
This is what worked for me (spring-boot v1.4.3.RELEASE),.
mvn spring-boot:run -Dspring.profiles.active=test,local -Dlogback-debug=true
For the latest version of spring use -Dspring-boot.run.arguments= as shown in the example below
spring-boot:run -Djasypt.encryptor.password=temp -Dspring-boot.run.arguments="OU,Grade"
Use the following command for a Spring Boot application.
mvn spring-boot:run -Dspring-boot.run.arguments="--java.net.preferIPv4Stack=true --config.password=PASSWORD --config.token=s.TOKEN --spring.application.name=ENV --config.server.ip=IP_ADDRESS --spring.profiles.active=ENV --spring.profiles.active.custom=ENV_custom"
I'm in the need to run integration tests one by one with the failsafe plugin (please, don't ask why... They just have to run in individual jvms, one good reason for avoiding singletons).
So I'd like to create a script with one maven call per line where each maven call runs tests on a single test file. Something like this:
mvn failsafe:integration-test -D<???>=**/ITest1.java
mvn failsafe:integration-test -D<???>=**/ITest2.java
mvn failsafe:integration-test -D<???>=**/ITest3.java
...
In a pom.xml I'd add something like
<includes>
<include>**/ITest1.java</include>
</includes>
to the plugins configuration. Is there a way to achieve the same with the command line? Is it possible to specify list-like configuration options?
The syntax is:
mvn -Dit.test=TestName verify
Source: Failsafe Plugin Examples: Running a Single Test
The it.test parameter does that. You can read about it here.
From the docs:
[...] so you can just type "-Dit.test=MyTest" to run a single test called "foo/MyTest.java".