Maven run from bash script ignoring profile? - java

I wrote a little script to compile the test version of a .jar and put it out on my test server.
cd /home/myusername/workspace/td-daily-budget
mvn -P test clean compile package
scp /home/myusername/workspace/td-daily-budget/target/td-daily-budget-1.0.jar myusername#666.666.666.666:/home/myusername/bin/td-daily-budget.jar
When I run it I get a jar file whose config.properties contains things like
db.connect.string=${db.connect.string}
but when I run mvn -P test clean compile package all by itself in the terminal window I get a jar file put together with the test profile, e.g. config.properties contains db.connect.string=[what I expect it to be for the dev profile]. Why does the same command ignore the profile when run inside a script?
Thanks in advance!
[edit/addendum]
Tried changing the script line to
/bin/bash mvn -P env-test clean package
(it's bash, not Windows, so there's no call command, but using bash to call another script seems to make sense to me) but just got
*Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher
*
I think calling it as an external process mucks up Maven's understanding of where it's supposed to be executing.
[2nd edit/addendum]
Removed the redundant compile from the command.
It now appears that this is actually an intermittent problem. The filtering almost always fails when mvn -P [pretty much ANY profile] clean package runs from inside the bash script, but if I run it standalone from the command line repeatedly it will work eight or nine times and then fail several times in a row. I cannot find any pattern to this at all.
As soon as I discovered this I thought I knew the culprit: The m2e plugin for Eclipse was "helping" me in the background every time it saw files changing. So I excitedly shutdown Eclipse thinking the problem would vanish instantly, ran mvn -P env-test clean compile package several times in a row--it worked the first few times and then failed. /headdesk
[edit/addendum]
Removed the space between the -P and the profile name. Still no luck.

I think you need to run the mvn in your batch with "call" command, so sth like:
call mvn -P test clean compile package
The reason is, that mvn is a batch file itself and thus needs to be invoked with "call".
Please give it a try!
Bluddy

Are you allowed to have a profile with the same name as a phase? Try renaming your "test" profile to "env-test" and use:
mvn -P env-test clean package
EDIT:
If you believe it has to do with calling it from a script, try adding a bash declaration at the top:
#!/bin/bash
cd /home/myusername/workspace/td-daily-budget
mvn -P env-test clean package
scp /home/myusername/workspace/td-daily-budget/target/td-daily-budget-1.0.jar myusername#666.666.666.666:/home/myusername/bin/td-daily-budget.jar
Credit Bluddymarri for suggesting the batch script equivalent.

Related

How to run Slenium+Java+Cucumber without ide

Hello there are tests written on Java + cucumber
The structure is as follows:
https://i.imgur.com/moLVY6L.png
The main question is how to run this tests not from the IDE, say from the console or even wrap it all in jar file
The problems encountered are that you need the main class to create a jar,
It seems as there is a certain java cucumber.api.cli.Main - but how to use it I do not understand. Either way, there's probably a way to run this just from the command line. Please tell us how to do it?
You can download Maven with following terminal script :
$brew install maven
After brew installation, you need to go to the project directory which includes pom.xml file in terminal :
$cd /path/of/your/project
And finally you can run following command to run your tests :
$mvn clean test
You can try the below on command prompt.
java -cp "E:\Workspace\CucumberProj\Jars*;E:\Workspace\CucumberProj\bin" cucumber.api.cli.Main --glue stepDefination E:\Workspace\CucumberProj\Feature\Login_Test.feature

How to run spring-boot application using GitLab runner?

I have a spring-boot maven application I've been running locally as:
mvn spring-boot:run
I want to be able to run this in GitLab runner so that when I push the code to master, it automatically copies the latest up there and runs the application.
My GitLab runner is configured in shell mode right now, and I have inside of the .gitlab-ci.yml file a deploy task that runs just that:
mvn spring-boot:run
The issue I am running into is after the application starts, I can see that it is running... but it never shows as success or completed. It just hangs there (because the terminal is still running when you execute that command?)
Question is, is there an alternate set of commands I should be running to get my spring-boot application to update and run each time I push to master? What is it i should be putting into my gitlab-ci.yml (or other files). Note that I am not using docker or kubernetes... just shell.
Sample gitlab CI:
run-deploy:
stage: deploy
script:
- mvn $MAVEN_CLI_OPTS spring-boot:run
Trying nohup with that also fails.
- nohup mvn $MAVEN_CLI_OPTS spring-boot:run &
I believe you can use the run stage for this. It would look something like
run:
stage: run
script:
- mvn $MAVEN_CLI_OPTS spring-boot:run
You can see an example of this here.
Make sure you also define the stages to include run as the docs state
If no stages are defined in .gitlab-ci.yml, then the build, test and deploy are allowed to be used as job’s stage by default. (see stages)
Is the sample file you provided above your entire configuration file or only a snippet of it? If so, I can adjust my answer to fit your needs. Thanks!
I'm sorry this is so late. I've recently just had the same problem. Gitlab runner blocks on child processes, and any process in the child tree. This makes the disown command impossible since you can't get to it. Forking, and nohup also don't work.
The only solution I could figure out was using the at command https://linux.die.net/man/1/at
Basically I put my command in a script then did:
at now < my_blocking_command_script.sh
That successfully complete the runner and kicked off my program in the background.

Java Cucumber - Run Junit Maven project in Command line

Trying to do some automated test. I have used below commands to execute the junit file (RunnerTest.java) in command line, getting an error as "could not find class"
Command line commands:
C:\Users\username\workspace\MavenCucumberPrototype\src\test\java\com\cucumber\MavenCucumberPrototype>javac -cp "C:/cjars/*" MavenCucumberPrototype/*.java
C:\Users\username\workspace\MavenCucumberPrototype>java -classpath C:/cjars/junit-4.12.jar org.junit.runner.JUnitCore src.test.java.com.cucumber.MavenCucumberPro
totype.RunnerTest
Maven Project structure
MavenCucumberPrototype
-/src/main/java
-com.cucumber.MavenCucumberPrototype
-/src/test/java
-com.cucumber.MavenCucumberPrototype
-postconn.java
-RunnerTest.java
-Steps.java
-/src/test/resource
-myfeature.feature
When I run Maven projects from a command line, I usually do
mvn clean install
or whatever lifecycle phase I want to execute. In your case, it might be sufficient to do
mvn test
The only imprtant thing to notice is that you must execute the command in the same directory as the pom.xml resides.
The error you recieve is nost likely due to a classpath that doesn't contain what you expected.

Using maven plugin exec-maven-plugin to run a shell script

I have a java web application project. I am using the exec-maven-plugin to execute a shell script which creates a small txt file in a directory when the project is built using mvn clean isntall. I have confirmed that the script is being executed when running mvn clean install by writing test text. However, the script is not creating the txt file.
When the script is run normally through the terminal, ie ./script.sh , the txt file is created correctly.
toTxt="hello"
printf $toTxt > testText.txt
echo 'This shows up, but testText is not created.'
Does anyone know the reason why this is happpening?
Try either echoing pwd command to see where it should create the file or add <workingDirectory>${project.build.outputDirectory}</workingDirectory>
to your configuration block in pom.xml
NOTE: You can specify something other than ${project.build.outputDirectory} to specifically point to the right place. And make sure you have write permissions to it.
Keep in mind that some commands are not really external programs, but shell built-ins. This means that they don't launch programs, and maven-exec-plugin will not be able to run them. However, you can run bash to get the result, just not pwd.
bash -c "echo $(pwd)"
should do the trick. Maven is launching bash which is then running the script echo $(pwd) which calls the bash builtin function pwd and passes the result back (due to the echo).
Yes, it's a lot of work to get around the lack of a pwd program, but that's because there really isn't a pwd program, it's part of the internal offerings of bash.
http://www.tldp.org/LDP/abs/html/internal.html lists the bash internals (builtin commands).

How create maven alias for set of goals?

I'm configuring a Maven project and want to be able to alias, like
mvn server - to execute mvn clean package tomcat:run
The Grunt task runner does it very well, but I haven't found a way to do the same in Maven. Is it possible?
You can define a <defaultGoal>...</defaultGoal> in your pom if you like. So you can define something like this:
<project>
<build>
<defaultGoal>clean package tomcat:run</defaultGoal>
.
</build>
</project>
will be activated if you simply call mvn...not really an alias, cause usually you don't need one...
Out of the box I don't know of any solution that doesn't imply using a plugin. A simple solution may be adding aliases to your .bashrc file in your home directory (for Linux) or .bash_profile (on OS X) for your desired instructions.
E.g: Adding a line alias my-alias="mvn clean install" will allow you to execute the command my-aliasin the terminal, obtaining the same result as running the mvn clean install instruction itself. Add another line alias my-alias-port="mvn clean install -Dcrx.port=9200"for a second instruction, and so on.
Optionally, you can execute alias to see a list of all your aliases and their respective instructions.
The best solution I have found to this is to use a combination of:
New Maven 3.3 command line config support: project/.mvn/maven.config
GNU Make (which then calls maven)
Bash scripts
Blaze
Roughly in that order of preference.
GNU Make is especially nice because it offers bash completion.
An example Makefile for your specific example would be:
.PHONEY: server
server:
[tab]mvn clean package tomcat:run
Replace [tab] with a real tab! See make documentation.
Then you can run:
make server
For windows environments you will need to install cygwin or something equivalent. For Mac you don't have to but you should probably install homebrew.
Finally the Maven Bash completion albeit doesn't do aliases but will greatly facilitate typing maven commands (press tab). Many package managers have this as a package (ie homebrew has it as maven-completion).
You can also add the following function to your .bashrc file:
function mvn() {
if [ "$1" = "i" ]; then
command mvn install
else
command mvn $#
fi
}
And so you can invoke the mvn install with the mvn i alias.
Everything else that is not mvn i will call the original mvn command instead.
Alias-maven-plugin is what you are looking for.
Following the site:
Whenever you type a command in a shell, for instance
mvn clean install
you could spare time in simply using an alias like this
i
It has also more advantages - you could configure plugin by XML file.

Categories

Resources