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.
Related
I am working on a project on a virtual machine that is running on windows 10, and where I don't have the user access to system variables in GUI. Since, I need to use Maven I am wondering how can I set the java and maven variables with bash terminal?
I have tried to set the variable with this command:
export M2_HOME="/c/Program Files/apache-maven-3.6.3"
export M2="$M2_HOME/bin"
export MAVEN_OPTS="-Xms256m -Xmx512m"
And then if ran mvn --version in the terminal, I got:
bash: mvn: command not found
If I ran printenv I see in the list that the variables were set:
M2_HOME=/c/Program Files/apache-maven-3.6.3
M2=/c/Program Files/apache-maven-3.6.3/bin
MAVEN_OPTS=-Xms256m -Xmx512m
But, if I close the terminal and open it again, then I don't see these variables again.
What is the right way to set up Java and Maven env variables through bash on Windows?
You need to escape the string by surrounding it with quotes:
export M2_HOME="/c/Program Files/apache-maven-3.6.3"
EDIT:
To answer the question in the comments, note that you still need to add mvn's path to the $PATH variable in order to use it:
export PATH=${PATH}:${M2}
I'm trying to create a maven project with VS Code but when I run the command it says :
'mvn' is not recognized as an internal or external command,operable program or batch file.
but mvn -version is running on command prompt
Environment varaible for User :
MAVEN_JOME : C:\apache-maven-3.6.1,
M2_HOME : C:\apache-maven-3.6.1,
JAVA_HOME : C:\Program Files\Java\jdk1.8.0_212\jre and
System Variables
path is set to C:\apache-maven-3.6.1\bin, C:\Program Files\Java\jdk1.8.0_212\bin for maven and java respectively.
The command I'm running to create the project is:
mvn archetype:generate -DgroupId=com.cs.test-project -DartifactId=test-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false The Reference to Command
And the VS Code Reference to Command
The same command is running in Command prompt and it created the project successfully on Desktop.
I fixed this problem by simply restarting VSCode as I had set my PATH variable while my VSCode was running so the changes were not reflected to the VSCode integrated terminal until I restarted it.
It's kindy counterintuitive as path has to be pointed to the actual mvn command, instead to a M2_PATH folder. Also once properly pointed, a new error will be shown if your environment is missing proper jdk within JAVA_HOME. The error is trown by maven. Maven plugin is kinda stupid as it will ignore your default JDK configured within settings.json (java.home or/and java.configuration.runtimes properties) and will happly NOT set java for maven. You have to configure it specificly for maven plugin as this:
"maven.executable.path": "c:\\apache-maven-3.8.1\\bin\\mvn",
"maven.terminal.customEnv": [{
"environmentVariable": "JAVA_HOME",
"value": "c:\\openjdk-1.8.0_232-redhat",
}]
Of course, both paths should be pointed to proper folders/files in your environment.
The way I fixed was by changing the exec path in Vs Code settings from CMD to Powershell.
Open
settings -> features -> terminal
Change the windows exec path from cmd to where you have PowerShell.
For example: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Windows exec setting
On Tuesday Oct. 25, 2022 while trying to solve the same problem, I went through all of these previous solutions for Windows and finally had to resort to the official installation docs:
Installing Apache Maven
Unzipped the download and located the bin directory and manually added that directory location to my: System Properties>Advanced>Environment Variables>'Path'
For good measure, restart the computer.
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.
Trying to setup jenkins but my builds fails with:
$ ant test
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
Build step 'Invoke Ant' marked build as failure
If I manually run "ant test" on the machine it works just fine and the JAVA_HOME is set to the exact same value. Any idea why it fails when jenkins try to run it ? Are there any more environment variabled involved ( I could not see any though ) ?
For others, I had to add the PATH /usr/bin/ to my PATH variable within Jenkins. (Find your correct path using which java).
Jenkins > Manage Jenkins > Configure System
Add an Environment Variable >>
e.g:
Name: PATH
Value: /usr/local/bin/:/usr/bin/
Screenshot
The problem was this, I had forgot to check the box "Restrict where this project can be run" in the project configuration. Thus the testing tried to execute on "master" where JAVA_HOME was not the same as expected on the build executor I intended to run it on. Thus where I tested and where it actually ran was different machines.
Set JAVA_HOME in your Jenkins system config in Jenkins 2.107.1.
open your jenkins, e.g. http://192.168.1.12:9090, then add /configure to the url,
that is http://192.168.1.12:9090/configure, then you can find like next:
For me the options above did not help, solved by creating a link to what's asked:
sudo ln -s ${actual_java_location} /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
actual_java_location can be read by this:
readlink -f $(which java)
When I am running mvn clean install for my build on linux RHEL 6. I'm getting the following error: java.lang.outOfMemoryError heap space.
I have read all the articles on internet. On my machine I dont have a file called mvn.sh, I only have a file mvn.bat.
Where can I set the export MAVEN_OPTS command?
You can run the mvn command, so its irrelevant wether you are using a .sh or .bat file. For future reference you should keep in mind though that .bat files are for Windows, not Linux. Anyway, in the same shell you are running your mvn command in, do this first:
export MAVEN_OPTS="-Xmx512M"
Then execute your mvn command. Bump the number up if you still run out of memory.
When I am running mvn clean install for my build on linux RHEL 6 it is showing java.lang.outOfMemoryError heap space.
You need to set the MAVEN_OPTS environment variable.
I have read all the articles on internet
That is false. At best you have read a TINY FRACTION of the relevant articles.
... and in my machine I don't have mvn.sh, I have mvn.bat
If you have used "yum" to install Maven, then there will be a "mvn" command on your command path. On my system, it is a shell script. If you were going to "hack" a script, that would what you would edit.
But you shouldn't need to.
and where to set export MAVEN_OPTS COMMAND.
This is a very basic "How to use a Linux command shell" question.
The answer is either you type is at the command prompt before you run the "mvn" command, or you add it to your shell initialization file and restart the shell as appropriate.
My advice would be to invest some time in reading a tutorial about how to use the Linux command shell. It will save you a lot of time in the long term.