How to run maven command in AWS Device Farm - java

I am trying to run my java mobile project in device farm using maven.
When I run this command on my local machine in the right directory it works:
mvn test -D "cucumber.options="src/test/java/cucumber/features" --glue
cucumber.steps --tags #InstallApp"
I put this command on the yaml file in the aws device farm but it doesn't work.
This is the error I get:
[DeviceFarm] echo "Start Appium TestNG test"
Start Appium TestNG test
[DeviceFarm] mvn test -D
"cucumber.options="src/test/java/cucumber/features" --glue
cucumber.steps
--tags #InstallApp"
[INFO] Scanning for projects...
[INFO] BUILD FAILURE
[ERROR] The goal you specified requires a project to execute but there
is
no POM in this directory (/tmp/scratchncneoU.scratch/test-
packageO6o8s7).
Please verify you invoked Maven from the correct directory. -> [Help 1]
How can I run this maven command in device farm? Where is the pom located there?
Thanks
After typing this command in the yaml file:
[DeviceFarm] java -Dappium.screenshots.dir=$DEVICEFARM_SCREENSHOT_PATH -D"cucumber.options="classpath:features" --glue cucumber.steps --tags #InstallApp" org.testng.TestNG -testjar *-tests.jar -d $DEVICEFARM_LOG_DIR/test-output -verbose 10
I get an error of: Couldn't find the testng.xml in the jar file although I have this in my pom:
<directory>${project.basedir}/src/test/java/cucumber</directory>
</testResource>
</testResources>
What can it be?

The mvn command fails because your deployment package doesn't contain the whole project. From the sound of things with the current deployment package you should be able to do what you want with this command:
- java -Dappium.screenshots.dir=$DEVICEFARM_SCREENSHOT_PATH -D"cucumber.options="src/test/java/cucumber/features" --glue cucumber.steps
--tags #InstallApp" org.testng.TestNG -testjar *-tests.jar -d $DEVICEFARM_LOG_DIR/test-output -verbose 10
Otherwise, if you want to use maven you'll need to zip up the whole project and include it's pom.xml. So for example using the sample cucumber java tests
git clone https://github.com/aws-samples/aws-device-farm-appium-cucumber-tests-for-sample-app.git
zip -r deployment_package.zip aws-device-farm-appium-cucumber-tests-for-sample-app
Then you should be able to use maven in the testspec.yml file. For example, I could run the following command for the sample: mvn clean test after going to the project dir and then to get the results I had to export the target directory.
test:
commands:
# Your test package is downloaded in $DEVICEFARM_TEST_PACKAGE_PATH so we first change directory to that path.
- echo "Navigate to test package directory"
- cd $DEVICEFARM_TEST_PACKAGE_PATH/aws-device-farm-appium-cucumber-tests-for-sample-app
- mvn clean test
...
artifacts:
# By default, Device Farm will collect your artifacts from following directories
- $DEVICEFARM_TEST_PACKAGE_PATH/aws-device-farm-appium-cucumber-tests-for-sample-app/target
- $DEVICEFARM_LOG_DIR
However, you'll need to upload the zip file as an APPIUM_NODE test type to avoid the test package parser there.
HTH
-James

Related

How to create and run a jar file with manifest file to support MySQL?

I'm trying to build and run a Java application that relies on 'mysql-connector-java-8.0.27.jar'. I currently to this with the following commands using Terminal on MacOS, which works fine, however it doesn't work on Windows, so I'm trying an alternative approach using Docker, which results in an MySQL driver error message when running the app.
Steps to manually build and run using Terminal.
step 1: find . -name "*.java" | xargs javac -d ./target
step 2: jar cfme my_app.jar manifest.txt Main -C ./target/ .
step 3: java -jar my_app.jar
The manifest file includes a reference to the MySQL jar-file:
Manifest-Version: 1.0
Class-Path: lib/mysql-connector-java-8.0.27.jar
As mentioned before, this solution works fine on Mac, but doesn't work on Windows.
So now I'm trying to use Docker to build and run the app instead. It was done this way before when the app didn't use MySQL and there was no need for the manifest file. I included steps to copy the manifest file in the Dockerfile as below, however when running the application I get an error: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3316/myDatabase. I didn't get this error when I built the app manually using the 3 steps I listed out above.
Contents of Dockerfile:
FROM openjdk:17-slim AS builder
COPY src /usr/src/app/src
COPY lib /usr/src/app/lib
COPY manifest.txt /usr/src/app/
WORKDIR /usr/src/app
RUN find . -name "*.java" | xargs javac -d ./target
RUN jar cfme my_app.jar manifest.txt Main -C ./target/ .
Steps to build and run the app using Docker:
1: docker build -t projects/my_app -f Dockerfile ./
2: docker run -ti projects/my_app sh
3: java -jar my_app.jar
How would I resolve the error or what would be a better approach to make this app more portable and easy to run on a different machine?
UPDATE 11/23:
I'm able to create a jar-file using Gradle, however when I run the application I get an error: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3316/myDatabase
This is the contents of of my build.gradle file:
plugins {
id 'java'
}
jar {
manifest {
attributes 'Main-Class': 'myApp.Main'
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'mysql:mysql-connector-java:8.0.27'
}
I'm new to Gradle, and appreciate any help with this. Thank you!

How to pass maven options to JaCoCo tool in Maven#3 task in Azure Devops

I am trying to build, run tests and publish JaCoCo code coverage reports using Azure Devops pipelines. I followed the documentation of the task provided in link here
The final task which i am running is something like this:
- task: Maven#3
displayName: Build Project
inputs:
mavenPomFile: 'pom.xml'
goals: 'clean package'
options: '-s settings.xml'
publishJUnitResults: true,
testResultsFiles: '**/surefire-reports/TEST-*.xml'
codeCoverageToolOption: 'jaCoCo'
....
This results tasks runs fails at the verify step. Below is the maven commands those get run:
/usr/share/apache-maven-3.6.3/bin/mvn -version
/usr/share/apache-maven-3.6.3/bin/mvn -f /home/vsts/work/1/s/pom.xml help:effective-pom -s settings.xml
/usr/share/apache-maven-3.6.3/bin/mvn -f /home/vsts/work/1/s/pom.xml -s /home/vsts/work/_temp/settings.xml clean package and finally
/usr/share/apache-maven-3.6.3/bin/mvn -f /home/vsts/work/1/s/pom.xml verify - FAILS
As you can see for step 2 and step 3 both have -s settings.xml in the command, but in case of step 4 (which is initiated by the JaCoCo part of the task), the option (-s settings.xml) is not passed because of which certain dependencies are not getting downloaded and the build is failing.
Can anyone provide me some insights as to how to propagate the maven args to the JaCoCo coverage mvn verify command. Thanks in advance for your help.

Unable to run 'RUN ./mvnw dependency:go-offline -B' when building docker image from "openjdk:8-jdk-alpine" for Spring Boot app

So I am trying to run a spring boot app with maven wrapper inside the docker container. Here is my Docker file:
### Stage 1: Build the application
FROM openjdk:8-jdk-alpine as build
RUN mkdir -p /app
#Set the current working directory inside the image
WORKDIR /app
#copy maven executable to the image
COPY mvnw .
COPY .mvn .mvn
#Copy the pom.xml file
COPY pom.xml .
#Build all the dependencies in preparation to go offline
#This is a separate step so the dependencies will be cached unless
#the pom.xml file has changed
RUN ./mvnw dependency:go-offline -B
#Copy the project source
COPY src src
#Package the application
RUN ./mvnw package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
I have this error:
Step 7/16 : RUN ./mvnw dependency:go-offline -B
---> Running in 642a32f86392
/bin/sh: ./mvnw: not found
ERROR: Service 'app-server' failed to build: The command '/bin/sh -c ./mvnw dependency:go-offline -B' returned a non-zero code: 127
I am working with windows 10 pro. Please I need your help
Maybe a duplicate of Unable to run './mvnw clean install' when building docker image based on "openjdk:8-jdk-alpine" for Spring Boot app
Can you check the line endings of the mvnw shell script?
You could fix it by adding this before executing the mvnw command:
RUN dos2unix mvnw
Alternatively, if the file is in git, you can also fix it by adding the following to a .gitattributes file and checking the file out again:
*.bat text eol=crlf
mvnw text eol=lf
You have to copy the project files into the /app dir first. And you don't have the maven wrapper in the context folder where you run the docker build.
Try change the end of line mvnw file from Windows style CRLF to Unix style LF. Then rebuild the image.

How to build multiple projects (maven and scala) using batch file

I am trying to build maven project as well as scala project together one by one using a windows batch file. For this I have created a batch file in the root folder and written following lines inside it. Although my maven projects are building successfully but the scala project are failing to build.
call mvn -f A/pom.xml clean install -U
call sbt B\build.sbt clean compile -U
call mvn -f C/pom.xml clean install -U
call sbt D\build.sbt clean compile -U
The error I am getting is:
[warn] Executing in batch mode.
[warn] For better performance, hit [ENTER] to switch to interactive mode,
or
[warn] consider launching sbt without any commands, or explicitly passing
'shell'
[info] Set current project to my project(in build file:/C:/sample-
project/MyProject/)
[error] Expected ID character
[error] Not a valid command: B
[error] Expected project ID
[error] Expected configuration
[error] Expected ':' (if selecting a configuration)
[error] Expected key
[error] Not a valid key: B
[error] B\build.sbt
[error] ^
[SOLUTION]
The following worked for me...
cd A
call mvn clean install -U
echo Completed building A
cd ..
cd B
call mvn clean install -U
echo Completed building B
cd ..
cd C
call sbt clean compile -U
echo Completed building C
cd ../
cd D
call mvn clean install -U
echo Completed building D
cd ..
cd E
sbt clean compile
echo Completed building E

Maven not found while using Gitlab-ci-multi-runner

I've been trying to set up GitLab-CI to build Maven Java projects in Raspbian, but whenever It tries to run the build script it fails because it can't find Maven. I used this guide to install the runner.
Build trace:
Running with gitlab-ci-multi-runner 1.10.4 (b32125f)
WARNING: image is not supported by selected executor and shell
Using Shell executor...
Running on raspberrypi...
Cloning repository...
Cloning into '/home/gitlab-runner/builds/f09314a8/0/RBian/TestProject'...
Checking out 54d2e140 as master...
Skipping Git submodules setup
$ mvn clean package
bash: line 22: mvn: command not found
ERROR: Build failed: exit status 1
.gitlab-ci.yml
image: maven:3-jdk-8
build:
script: "mvn clean package"
artifacts:
name: "TestProject"
paths:
- "target/*.jar"
Does anyone know what causes this?
EDIT: Modifying /etc/default to add $MAVEN_HOME/bin to $PATH did the trick.
Try adding MAVEN_HOME path into config.toml environment field.
environment = ["MAVEN_HOME=/path/to/maven"]

Categories

Resources