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.
Related
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
I am using Jenkins, and using a Github repo as Source Code.
In the Build section, I am executing this as a Windows Batch command:
set path=%path%;C:\Program Files\Java\jdk1.8.0_144\bin
cd \Users\harr\JenkinsServer\JenkinsTest\src
javac SimpleTest.java //Error is after this executes
java SimpleTest
I know it has something to do with classpath, but I am unsure how to solve this problem in jenkins.
Let me know if more information would be helpful.
Suppose you deploy the jekins server on linux platform, so you have to install the jdk, tomcat and so on, set the env path as well. Then you don't have to execute set path before every build.
you can create a script and copy the command into it, then when jenkins performs the build task, it can execute the script. Refer to the jenkins tutorial to learn about it.
Maven has a capability to perform parallel builds:
https://cwiki.apache.org/confluence/display/MAVEN/Parallel+builds+in+Maven+3
mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core
Is it possible to specify this arguments in pom.xml or settings.xml? Repeating this options could be annoying.
This solution is a bit of a hack but worked for me. It involves specifying a new environment variable, assigning the value -T3 to it and adding this variable to the Maven launch script.
For Windows (Linux in parens):
Open the Environment Variables window: Computer -> Properties -> Advanced System settings -> Environment Variables
Add the property MAVEN_CMD_LINE_OPTS with your desired value. In my case -T 3 as I want Maven to use 3 threads to build in parallel.
Edit the mvn.cmd file (In Linux: the mvn file). Find the part where the Java command is actually executed, the line starting with %MAVEN_JAVA_EXE% (In Linux: generally after the line defining the main class: org.codehaus.plexus.classworlds.launcher.Launcher)
Add %MAVEN_CMD_LINE_OPTS% to the end of the line (In Linux: $MAVEN_CMD_LINE_OPTS)
When you run mvn compile on a Maven project you will now see the following line:
Using the MultiThreadedBuilder implementation with a thread count of 3
This has the advantage of the user being able to 'override' this value. So if the user executes mvn -T4 compile, then 4 threads are used instead of the default 3.
Note:
I tried this on Maven 3.3.9 but the concept should work on any Maven
version.
Multi-threaded builds can suffer from issues where plugins
especially custom plugins are not thread safe. So use with care and
consider disabling this as a fix in case of issues.
I could not find a way to configure this in the pom.xml or settings.xml There is a good solution on unix systems, edit your .bashrc and add an alias.
alias mvnp='mvn -T 4'
now from the terminal run maven using mvnp
mvnp clean install
You are able to specify the option in the MAVEN_OPTS environment variable (see http://maven.apache.org/guides/mini/guide-configuring-maven.html). Once this is done, you don't have to repeat it. Configuring the environment variable depends on your system. However this will affect all maven runs within your environment. Maybe it's possible for you to enable different environments, so that only the project you actually want to build in parallel is running in such an environment.
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.
rI want to run jetty:run in debug mode with MAVEN_OPTS setted in environment variable. But it seams like hardcode MAVEN_OPTS. Is it possible to set MAVEN_OPTS in command line like mvn MAVEN_OPTS=...
Thank you.
Is it possible to set MAVEN_OPTS in command line like mvn MAVEN_OPTS=...
No, MAVEN_OPTS is an environment variable, you can't set it on the command line. But you there is an alternative. Instead of mvn, you can simply run mvnDebug (a little variation of the former script that set debug options):
$ mvnDebug jetty:run
Preparing to Execute Maven in Debug Mode
Listening for transport dt_socket at address: 8000
I find this alternative pretty handy, and easier.
Under Windows - I don't know. Under Linux/Bash - yes you can:
export MAVEN_OPTS="-Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
mvn jetty:run
Under Windows you should be able to do the following from the command prompt:
set MAVEN_OPTS=<options you want to add> %MAVEN_OPTS%
mvn jetty:run
Under Mac/Linux/Unix you can use export from the Terminal:
export MAVEN_OPTS=<options you want to add> $MAVEN_OPTS
mvn jetty:run
Not sure about how to do single use exports in Windows, but on Unix like operating systems you can just prepend the variable to your command (this works for any environment variable you want to add).
MAVEN_OPTS="option1 option2" mvn jetty:run
I encountered this problem, and my solution was to create a .bat file to set the maven opts, and then start jetty.
call set MAVEN_OPTS=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=8484,server=y,suspend=n %MAVEN_OPTS%
call mvn jetty:run-war -DskipTests=true
My IDE of choice is Eclipse, so I use the run button with the tool box to call the .bat files. Here is a question on running a .bat file.