I am new with java and start to reading maven but the document is not clear for me. I have a simple Hello-World project like so :
package main;
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
I want to implement this in maven structure .What should I do for this ?
I download and install appache-maven-3.3.3-bin.zip and set the environment variable .
See this page.
Using this command:
mvn -B archetype:generate \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DgroupId=com.mycompany.app \
-DartifactId=my-app
It will generate a new directory my-app that is a complete Maven project with the recommended layout, that is:
$ find my-app/
my-app/
my-app//pom.xml
my-app//src
my-app//src/main
my-app//src/main/java
my-app//src/main/java/com
my-app//src/main/java/com/mycompany
my-app//src/main/java/com/mycompany/app
my-app//src/main/java/com/mycompany/app/App.java
my-app//src/test
my-app//src/test/java
my-app//src/test/java/com
my-app//src/test/java/com/mycompany
my-app//src/test/java/com/mycompany/app
my-app//src/test/java/com/mycompany/app/AppTest.java
Customize the groupId and artifactId according to your needs.
See Introduction to the Standard Directory Layout for more details about the layout.
Related
I have a Java project with some Cucumber tests and some regular JUnit tests, managed by Maven.
I want to run the tests in Jenkins, using Docker, so I wrote this Jenkinsfile:
pipeline {
agent {
docker {
image 'maven:3.6.1'
}
}
stages {
stage('build') {
steps {
sh 'mvn clean verify -Dmaven.test.failure.ignore=true'
}
post {
success {
junit 'target/surefire-reports/**/*.xml'
}
}
}
}
}
When I run the build, the regular tests pass but the Cucumber tests fail with:
Error Message
URI has a query component
Stacktrace
java.lang.IllegalArgumentException: URI has a query component
It's not until I disable trimStackTrace for maven-surefire-plugin that I also get the details:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
java.lang.IllegalArgumentException: URI has a query component
at java.base/java.io.File.<init>(File.java:427)
at cucumber.runtime.io.ZipResourceIterator.<init>(ZipResourceIterator.java:22)
at cucumber.runtime.io.ZipResourceIteratorFactory.createIterator(ZipResourceIteratorFactory.java:24)
at cucumber.runtime.io.ZipThenFileResourceIteratorFactory.createIterator(ZipThenFileResourceIteratorFactory.java:22)
at cucumber.runtime.io.DelegatingResourceIteratorFactory.createIterator(DelegatingResourceIteratorFactory.java:49)
at cucumber.runtime.io.ClasspathResourceIterable.iterator(ClasspathResourceIterable.java:35)
at cucumber.runtime.io.ResourceLoaderClassFinder.getDescendants(ResourceLoaderClassFinder.java:25)
at cucumber.runtime.Reflections.instantiateSubclasses(Reflections.java:34)
at cucumber.runtime.BackendModuleBackendSupplier.loadBackends(BackendModuleBackendSupplier.java:52)
at cucumber.runtime.BackendModuleBackendSupplier.get(BackendModuleBackendSupplier.java:39)
at cucumber.runner.ThreadLocalRunnerSupplier.createRunner(ThreadLocalRunnerSupplier.java:42)
at cucumber.runner.ThreadLocalRunnerSupplier.access$000(ThreadLocalRunnerSupplier.java:13)
at cucumber.runner.ThreadLocalRunnerSupplier$1.initialValue(ThreadLocalRunnerSupplier.java:22)
at cucumber.runner.ThreadLocalRunnerSupplier$1.initialValue(ThreadLocalRunnerSupplier.java:19)
at java.base/java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:195)
at java.base/java.lang.ThreadLocal.get(ThreadLocal.java:172)
at cucumber.runner.ThreadLocalRunnerSupplier.get(ThreadLocalRunnerSupplier.java:38)
at cucumber.api.junit.Cucumber$RunCucumber.evaluate(Cucumber.java:146)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
This is quite puzzling because:
when running mvn verify locally, it works
when using agent any in the Dockerfile, instead of the maven Docker image, it works
when using maven-surefire-plugin version 2.22.2 instead of 3.0.0-M3, it works
I logged in as Jenkins and looked in the workspace:
$ sudo su - jenkins
$ ls /var/lib/jenkins/workspace/question-mark-dir_master
'?' Jenkinsfile LICENSE pom.xml README.md src target
There's a question mark (?) directory in there, and it contains the .m2 directory, with all the artifacts needed by the project.
I suspect this could be the cause of the exception, because then there are question marks in the classpath, and question marks are the ones who introduce the query component of URIs.
For the moment I can downgrade to maven-surefire-plugin version 2.22.2 as a workaround.
But what is the actual problem here?
Could it be a bug in the new maven-surefire-plugin? Judging by the stack trace I wouldn't bet on that.
Is it a problem in Jenkins or in the Maven docker image (which one?), that causes a directory to have such an unfortunate name?
Am I doing something wrong?
If you want to try this out, I have a MVCE.
In my local Jenkins installation, I created a new project of type "Multibranch Pipeline" and in "Branch Sources" I added as the git project repository my local git project. No other changes.
By combining TYY's comment to the question and the documentation of the maven docker image, I was able to configure the Jenkinsfile in a way that avoids creating a question mark directory.
We need to inform Maven of the user's home directory, and map that to a directory outside the container.
pipeline {
agent {
docker {
image 'maven:3.6.1'
args '-v /var/lib/jenkins:/usr/src/mymaven -w /usr/src/mymaven'
}
}
stages {
stage('build') {
steps {
sh 'MAVEN_OPTS="-Duser.home=/usr/src/mymaven" mvn clean verify -Dmaven.test.failure.ignore=true'
}
// ...
}
}
}
-v /var/lib/jenkins:/usr/src/mymaven maps the /var/lib/jenkins directory on the host to the /usr/src/mymaven directory inside the container
-w /usr/src/mymaven sets the working directory inside the container
MAVEN_OPTS="-Duser.home=/usr/src/mymaven" sets the user.home Java property for Maven
Proof that it works, on a branch of the MCVE.
Hello I'm new to jenkins and getting this issue. I'm using jenkins in windows azure
mvn clean package /var/lib/jenkins/workspace/vcc#tmp/durable-b5407f14/script.sh: 2:
/var/lib/jenkins/workspace/vcc#tmp/durable-b5407f14/script.sh: mvn:
not found.
Jenkinsfiles:
node {
stage('init') {
checkout scm
}
stage('build') {
sh '''
mvn clean package
cd target
cp ../src/main/resources/web.config web.config
cp todo-app-java-on-azure-1.0-SNAPSHOT.jar app.jar
zip todo.zip app.jar web.config
'''
}
stage('deploy') {
azureWebAppPublish azureCredentialsId: env.AZURE_CRED_ID,
resourceGroup: env.RES_GROUP, appName: env.WEB_APP, filePath: "**/todo.zip"
}
}
can any body help me how can I resolve this mvn issue.
P.S I'm following this tutorial
https://learn.microsoft.com/en-us/azure/jenkins/tutorial-jenkins-deploy-web-app-azure-app-service
You may try to add maven tool to your pipeline:
tools {
maven 'M3'
}
stages {
stage('init') {
checkout scm
}
stage('build') {
sh '''
mvn clean package
cd target
cp ../src/main/resources/web.config web.config
cp todo-app-java-on-azure-1.0-SNAPSHOT.jar app.jar
zip todo.zip app.jar web.config
'''
}
stage('deploy') {
azureWebAppPublish azureCredentialsId: env.AZURE_CRED_ID,
resourceGroup: env.RES_GROUP, appName: env.WEB_APP, filePath: "**/todo.zip"
}
}
I add this line right before sh command in the build stage : def mvnHome = tool name: 'Apache Maven 3.6.0', type: 'maven'
and instead of mvn you should use ${mvnHome}/bin/mvn
thank this youtube film to help me.
pipeline{
stage('com'){
def mvnHome = tool name: 'Apache Maven 3.6.0', type: 'maven'
sh "${mvnHome}/bin/mvn -B -DskipTests clean package"
}
}
You may wanna check if Jenkins has the pipeline-maven plugin installed.
If you don't have it, search and install the pipeline-maven plugin.
Once the plugin is installed, you can use maven as follows
node{
stage('init'){
//init sample
}
stage('build'){
withMaven(maven: 'mvn') {
sh "mvn clean package"
}
}
}
I have a git repository with 2 modules in it. One is SpringBoot based backend module and another one is VueJS based frontend module.
app-root
- backend
- frontend
I have a declarative style Jenkinsfile to build my backend module with relevant maven commands. I want to execute all those maven commands from inside backend directory.
One option is to use dir("backend") { ....} for all commands separately, which looks ugly to me.
Is there any other option to instruct Jenkins to execute the entire pipeline from inside a sub-directory?
I ended up with a "prepare project" stage that puts the subdirectory content into the root.
It's also probably a good idea to remove all the root contents (stage "clean") to be absolutely sure there are no leftovers from previous builds.
node {
def dockerImage
stage('clean') {
sh "rm -rf *"
}
stage('checkout') {
checkout scm
}
// need to have only 'backend' subdir content on the root level
stage('prepare project') {
// The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.
// The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.
sh "cp -a ./backend/. ."
sh "rm -rf ./backend"
// List the final content
sh "ls -la"
}
stage('build docker image') {
dockerImage = docker.build("docker-image-name")
}
stage('publish docker image') {
docker.withRegistry('https://my-private-nexus.com', 'some-jenkins-credentials-id') {
dockerImage.push 'latest'
}
}
}
You can have jenkinsfiles inside backend and frontend modules and just point to them on each pipeline, eg:
and on the pipeline itself you just cd to the submodule and execute its commands:
pipeline {
agent any
stages {
stage('build') {
steps {
sh 'cd backend'
sh 'mvn clean package'
}
}
//other stages
}
}
if you don't want to cd to a sub module you can use sparse checkouts but in this case you have to change the path to the jenkinsfile accordingly because it will be on the root folder.
I'm running ./run.sh from Alfresco All-in-One extension but I got the error:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:3.2:compile
(default-compile) on project repo-amp: Compilation failure:
Compilation failure:
[ERROR]
/home/user/signextension/sign/repo-amp/src/main/java/pt/empt/sign/fields/CreateFields.java:[3,25]
package com.itextpdf.text does not exist
[ERROR]
/home/user/signextension/sign/repo-amp/src/main/java/pt/empt/sign/fields/CreateFields.java:[4,25]
package com.itextpdf.text does not exist
[ERROR]
/home/user/signextension/sign/repo-amp/src/main/java/pt/empt/sign/fields/CreateFields.java:[5,1]
package com.itextpdf.text.pdf does not exist
My run.sh:
#!/bin/bash
# Downloads the spring-loaded lib if not existing and runs the full all-in-one
# (Alfresco + Share + Solr) using the runner project
springloadedfile=~/.m2/repository/org/springframework/springloaded/1.2.3.RELEASE/springloaded-1.2.3.RELEASE.jar
if [ ! -f $springloadedfile ]; then
mvn validate -Psetup
fi
MAVEN_OPTS="-javaagent:$springloadedfile -noverify -Xms256m -Xmx2G" mvn clean install -Prun
I try this:
#!/bin/bash
# Downloads the spring-loaded lib if not existing and runs the full all-in-one
# (Alfresco + Share + Solr) using the runner project
springloadedfile=~/.m2/repository/org/springframework/springloaded/1.2.3.RELEASE/springloaded-1.2.3.RELEASE.jar
itextpdffile=~/.m2/repository/com/itextpdf/itextpdf/5.5.7/itextpdf-5.5.7.jar
if [ ! -f $springloadedfile ] && [ ! -f $itextpdffile ]; then
mvn validate -Psetup
fi
MAVEN_OPTS="-javaagent:$springloadedfile -javaagent:$itextpdffile -noverify -Xms256m -Xmx2G" mvn clean install -Prun
But I got the error:
Failed to find Premain-Class manifest attribute in
/home/user/.m2/repository/com/itextpdf/itextpdf/5.5.7/itextpdf-5.5.7.jar
Error occurred during initialization of VM agent library failed to
init: instrument
Any help for solve this?
The itext library is not a javaagent library, check this link to learn more about java agents and what are they meant for !
The right way to add itext dependency to your project is by adding this snippet:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.7</version>
</dependency>
To your main pom.xml file (or the repo-amp pom.xml alternatively) (in the dependencies section). And of course roll back any changes you made to the run.sh file !
I imported Example from how to use storm book
and when i run it i got this
INFO] An exception occured while executing the Java class. null 0
i used this command in Terminal
mvn -f pom.xml compile exec:java -Dstorm.topology=TopologyMain
Code:
import spouts.WordReader;
import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.tuple.Fields;
import bolts.WordCounter; import bolts.WordNormalizer;
public class TopologyMain {
public static void main(String[] args) throws InterruptedException {
//Topology definition
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("word-reader",new WordReader());
builder.setBolt("word-normalizer", new WordNormalizer()) .shuffleGrouping("word-reader");
builder.setBolt("word-counter", new WordCounter(),1) .fieldsGrouping("word-normalizer", new Fields("word"));
//Configuration
Config conf = new Config(); conf.put("wordsFile", args[0]); conf.setDebug(false);
//Topology run
conf.put(Config.TOPOLOGY_MAX_SPOUT_PENDING, 1);
LocalCluster cluster = new LocalCluster(); cluster.submitTopology("Getting-Started-Toplogie", conf, builder.createTopology());
Thread.sleep(1000);
cluster.shutdown();
}
}
I also tried this:
mvn -f pom.xml clean install
then tried to use this
mvn exec:java -Dexec.mainClass="TopologyMain" -Dexec.args="src/main/resources/words.txt"
error here
[0] Inside the definition for plugin 'exec-maven-plugin' specify the following:
<configuration> ... <mainClass>VALUE</mainClass></configuration>
You're using the -D argument incorrectly. It should be instead:
mvn -f pom.xml compile exec:java -Dexec.mainClass=storm.topology.TopologyMain
This will specify the main class to execute. It should be packaged with package storm.topology, which is not evident in the code you pasted.
Also, I don't know why you're explicitly specifying your POM file. You should create a pom.xml file in the project's root directory, and then you won't have to specify it on the command-line. Ideally you should be typing,
mvn clean install
mvn exec:java -Dexec.mainClass="storm.topology.TopologyMain"
This will clean your project, compile it, install any dependencies, and then execute the project with TopologyMain as the entry point.