Setting environment variables for application Maven - maven

My question is i have a pom file which calls a bat file which sets environment variables for the application. Then i call the ant script which uses these environment variables to compile and execute. These environment variables cant be recognized by ant script and it fails. I believe its because both run in different context. Can you guys let me know how to link this together.
org.codehaus.mojo
exec-maven-plugin
1.1
Pre_Clean
pre-clean
exec
env.bat
maven-antrun-plugin
Compile
compile
run

Yes. When the batch file ends the environment changes go out of scope. So, you need to call ant from within your .bat file OR set up the environment before starting maven OR set them up in the POM.

Related

How can I specify Maven command line arguments system wide on Windows?

How can I alter the default arguments passed to the mvn executables executed on a Windows system without modifying each installation or manually creating alternative startup scripts (or otherwise "hard-coding" my preferences in a non-portable way)? To be clear, I am not open to external methods like a console alias or using a shortcut or filesystem link. I am asking for a Maven-specific portable way to describe the default startup arguments for all Maven executions on my Windows machine.
Much like MAVEN_OPTS allows you to pass command line arguments to the JVM used to run Maven, MAVEN_CMD_LINE_ARGS is an environmental variable that allows you to pass command line arguments directly to Maven itself (but only on Windows). Unlike modifying files and settings at the level of an individual Maven installation or project (via settings.xml or maven.config for instance), MAVEN_CMD_LINE_ARGS allows you to pass command line arguments to any Maven installation spun up using the provided startup scripts (on Windows the scripts are "mvn.cmd" and "mvnDebug.cmd").
As an example, on my Windows development computer I have set MVN_CMD_LINE_ARGS to
--show-version --global-settings %M2_CONF%\settings.xml --global-toolchains %M2_CONF%\toolchains.xml --define settings.security=%M2_CONF%\security-settings.xml --fail-fast --update-snapshots --strict-checksums --define maven.artifact.threads=8.
Instead of having to place an alias or create my own startup script I simply can use this builtin method to achieve my goal of standardized cross-installation configurations. With MVN_CMD_LINE_ARGS set to that value, executing
mvn
on the command line is actually like executing
mvn --show-version --global-settings %M2_CONF%\settings.xml --global-toolchains %M2_CONF%\toolchains.xml --define settings.security=%M2_CONF%\security-settings.xml --fail-fast --update-snapshots --strict-checksums --define maven.artifact.threads=8.
Now, notice I said that I believe this only functions on Windows. While the Linux Bash scripts include an export of a variable called MVN_CMD_LINE_ARGS they do not then pass it to the mvn executable as an argument. The result of this is that on both Windows and Linux you can use MVN_CMD_LINE_ARGS from after the execution to determine what arguments were used to call mvn, but only on Windows can you use MVN_CMD_LINE_ARGS to instruct which arguments will be used to call mvn. From what I can tell though, this behavior may not be intended, so I would not recommend using this in a critical manner. It seems there is a project specific way to configure the mvn execution arguements via placing them into a "./.mvn/maven.config" file within the projects directory structure.

How to set environment variable using Gradle?

I know I can't set environment variable directly from Gradle, but is there some other solution?. I need to do this:
do some stuff..
set 4 environment variables (their values depend on some settings)
run some ant scripts (that depend on environment variables)
I thought of creating a file (.setenvironment) with all the settings I need, and then source it from Gradle (source .setenvironment), but I fear that I wouldn't be able to reset the variables if something goes wrong (and I need to set "JAVA_HOME", for instance, which is also important for the build scripts themself).
You can't set environment variables from java and other JVM languages. The only way of doing it is via ProcessBuilder.
You can also set the variables before gradle is run.

How do I set the path to the build.xml file in a Jenkins ant build from an environment variable on Windows

I am running an ant build in Windows from Jenkins and wish to pass the path to the build.xml file in an environment variable. I am using the envinject plugin to do so. I wish to set the variable on the project under "Prepare an environment for the run"->"Properties Content":
ANTBUILDDIR=C:\some\directory\here
Then in the ant build properties I set:
"Build File": %ANTBUILDDIR%\build.xml
Try $ANTBUILDDIR/TheBuildFile.xml as Jenkins configuration follows Unix shell variable expansion syntax.

Ant set platforms.JDK_1.6.home without parameter

I'm having a problem trying to run ant on a windows machine. I get the following error:
BUILD FAILED
C:\Users\USER\testing\mercurial\project\NetbeansProject\nbproject\build-impl.xml:111: The J2SE Platform is not correctly set up.
Your active platform is: JDK_1.6, but the corresponding property "platforms.JDK_1.6.home" is not found in the project's properties files.
Either open the project in the IDE and setup the Platform with the same name or add it manually.
For example like this:
ant -Duser.properties.file=<path_to_property_file> jar (where you put the property "platforms.JDK_1.6.home" in a .properties file)
or ant -Dplatforms.JDK_1.6.home=<path_to_JDK_home> jar (where no properties file is used)
If I execute ant -Dplatforms.JDK_1.6.home=%JAVA_HOME% it executes fine, but, is there some way to avoid adding this parameter every time I need to build a program?
I don't think it's possible to set ant properties outside of command line or properties files loaded explicitly by the build script.
If you are looking for a less verbose way to launch ant, try using either a wrapper .bat file, or assigning an alias doskey ant=ant "-Dplatforms.JDK_1.6.home=%JAVA_HOME%"

how do I import my intellij project settings on a groovy script in terminal

I have a single script in a project in intellij that I want to run in my mac terminal. I can run the script but I am getting errors where it can't find the JAR files I added as a module. There are a lot of dependencies, so I was wondering how can I add my intellij project settings to calling my groovy script. Right now all I have is this:
groovy CreateReport.groovy
Turns out all you need to do is dump your libraries into the GROOVY_HOME/lib directory, where GROOVY_HOME is where you have defined your groovy environment variables to be.
http://dustinwhitney.blogspot.com/2008/03/groovy-classpath.html

Resources