Setting Environment Variables in Maven pom files - maven

Sorry for asking a question that many will say it has been asked again and again, however I couldn't find a satisfactory or complete answer.
I have to build a maven project. Inside it I have the following:
<profile>
<id>windows</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<properties>
<weblogic.home>${env.WEBLOGIC_SERVER_HOME}</weblogic.home>
<weblogic.precompilation.java.home>${env.JAVA_64_HOME}</weblogic.precompilation.java.home>
</properties>
</profile>
and I get the error:
[ERROR] 'dependencyManagement.dependencies.dependency.systemPath' for weblogic:weblogic.jar must specify an absolute path but is ${env.WEBLOGIC_SERVER_HOME}/server/lib/weblogic.jar # ..\pom.xml
Of course, by setting the absolute path to the above:
<weblogic.home>c:/Oracle/Middleware/wlserver_10.3</weblogic.home>
the problem is solved, but how can I actually set env.WEBLOGIC_SERVER_HOME in my system so that maven finds it?
I setup a new environment variable:
WEBLOGIC_SERVER_HOME = c:/Oracle/Middleware/wlserver_10.3
in my Windows machine but no luck. I tried setting configuration environmentVariables in maven-surefire-plugin as explained in [How to set up an environment variable in mvn pom? (but didn't pass it via -D in the command line), tried to set a property like here [How to refer environment variable in POM.xml? but no chance.
I thought that once I setup the environment variable (WEBLOGIC_SERVER_HOME) in my OS, I could then access it from my pom using ${env.WEBLOGIC_SERVER_HOME}. But I guess this is not enough.
In my IDE (NetBeans), I cannot set the environment variable for the maven project since it displays the "Resolve Project Problems" dialog box instead.
Any help is appreciated.

Related

Goal specific javax.net.ssl.trustStore

I've got a Maven project for which i use org.apache.tomcat.maven:tomcat6-maven-plugin to deploy to a remote Tomcat.
This tomcat is configured so that i need to specify:
-Djavax.net.ssl.trustStore=mykeystore.jks -Djavax.net.ssl.trustStorePassword=mypassword
My problem is that by doing so, I can't download dependencies anymore from remote repositories through my company proxy as it tries to establish a secure connection using this truststore and it fails...
I'm looking for a way to connect to both ends (maven repo and my remote tomcat) without having to set/unset my MAVEN_OPTS variable every time...
I've seen that I can have a <configuration /> element in my settings.xml, but I can't find what to put in it.
Thanks...
Using different profiles with maven:
Define the active profiles in your pom (you can also define profiles in settings.xml but I think this should work for your case):
<profiles>
<profile>
<id>TOMCAT_DEPLOY</id>
<activation>
// Rules to active the profile
<activeByDefault>true</activeByDefault>
</activation>
<properties>
</properties>
// Add rest of profile specific configuration
</profile>
</profiles>
For executing maven with an specific profile, basically you have a list of active profiles and you can execute one of them according to different triggers:
A profile can be triggered/activated in several ways:
Explicitly, Through Maven settings, Based on environment variables OS settings or based on some Present or missing files
Please, read this link where you can have all information about profiles and how activate them for any execution

Is it possible to use a maven property to activate a profile based upon a file?

I would like to download the JACOB dlls when they're not in my local repository.
As a consequence, I have those two profiles
<profile>
<id>use-jacob-dll</id>
<activation>
<file>
<exists>${settings.localRepository}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>${jacob.groupId}</groupId>
<artifactId>jacob</artifactId>
<type>dll</type>
<classifier>x64</classifier>
<version>${jacob.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>download-jacob-dll</id>
<activation>
<file>
<missing>${settings.localRepository}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</missing>
</file>
</activation>
But, even when download-jacob-dll has accomplished its goal, a call to mvn help:active-profiles indicates the following
The following profiles are active:
- tests-for-eclipse (source: com.capgemini.admdt:kpitv:1.2.4-SNAPSHOT)
- download-jacob-dll (source: com.capgemini.admdt:kpitv:1.2.4-SNAPSHOT)
I suspect it is due to the fact that I use the ${settings.localRepository} in my activation property.
Question: Is it the cause of the failure? And if so, how can I activate my profile only when dependency is missing ?
Is it possible to use a maven property to activate a profile based upon a file?
No, as stated by the Maven documentation on profiles
Supported variables are system properties like ${user.home} and environment variables like ${env.HOME}. Please note that properties and values defined in the POM itself are not available for interpolation here, e.g. the above example activator cannot use ${project.build.directory} but needs to hard-code the path target.
However, from the POM documentation we also get that
a given filename may activate the profile by the existence of a file, or if it is missing. NOTE: interpolation for this element is limited to ${basedir}, System properties and request properties.
Hence, indeed no Maven properties except ${basedir} are allowed.
And if so, how can I activate my profile only when dependency is missing?
By hardcoded path to the dependency or concerned file would be a solution, even though not portable like the solution you meant.
Alternatively you could use a request property as mentioned by the documentation above, thus need to configure the activation with a property which then must be passed from the command line (more portable but more fragile as well):
<activation>
<file>
<missing>${path}/com/hynnet/jacob/1.18/jacob-1.18-x64.dll</missing>
</file>
</activation>
Then invoke maven as following:
mvn clean install -Dpath=path_to_local_rep
The solution above could be reasonable in some contexts like Jenkins jobs.

Conditional processing in Maven projects

I have some configuration settings that are in the Maven project that I'd like to keep there for building into a newly set up system but not touched if the configuration setting already exists. One of the problems I had is that when I deleted the setting from the build, it also deleted the setting from the target server when I did a build.
What would good alternatives be?
A possible solution could be to move the particular set-up for the new systems in a Maven profile and activate the profile only based on either an environment variable or the existence/non-existence of a file.
For instance, from official documentation, you can activate the profile if a certain environment variable exists, not exists or exists with a certain value.
<profiles>
<profile>
<activation>
<property>
<name>environment</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
Please also note that, as from official documentation:
Note: Environment variables like FOO are available as properties of the form env.FOO. Further note that environment variable names are normalized to all upper-case on Windows.
Alternatively, the profile can be activated on a certain file (existing or missing), as following:
<profiles>
<profile>
<activation>
<file>
<missing>path/to/missing/file</missing>
</file>
</activation>
...
</profile>
</profiles>
If your set-up cannot be applied to any of these two cases, you could (as an example) anyway adapt it to create an harmless file which would then deny any further set-up.
harmless file missing > profile activated > set-up performed
harmless file existing > profile not activated

Netbeans build failure

Brand new to netbeans and working on the address-book tutorial. I am able to run the program without issues but when I build I get the following error. I have yet to find any possible solutions to the issue
Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy (deploy) on project address-book: Execution deploy of goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy failed: Failed to create deployer with implementation class org.codehaus.cargo.container.glassfish.GlassFish4xInstalledLocalDeployer for the parameters (container [id = [glassfish4x]], deployer type [installed]). InvocationTargetException: The container configuration directory "c://glassfish4/glassfish/domains" does not exist. Please configure the container before attempting to perform any local deployment. Read more on: http://cargo.codehaus.org/Local+Configuration -> [Help 1]
How does Maven know which server to deploy to and where is it located?
It's all in the pom.xml file.
Sometimes you will have to trace up the parents of the pom.xml to find it.
In the case of Java EE tutorial, when I was building project hello1, Maven couldn't find the location of my GlassFish server.
I traced up the parents of pom.xml to find
C:\mine\tools_installation\glassfish4\docs\javaee-tutorial\examples\pom.xml(51):
<glassfish.home.prefix>c:/</glassfish.home.prefix>
Then I changed it to
<glassfish.home.prefix>c:/mine/tools_installation</glassfish.home.prefix>.
The container configuration directory "c://glassfish4/glassfish/domains" does not exist.
Maven / Cargo looks for a folder defined in pom.xml and it doesn't exist on your computer.
I the property you need to change is and you'll need to set it to your GlassFish installation directory.
So it is automatically taken from the parent examples folder pom.xml rather than from your open example. So you should edit the pom.xml from
YOUR_GLASSFISH_HOME\docs\javaee-tutorial\examples
It think it is a bug in the examples pom.xml
I encountered similar error, the solution is very simple You need to edit the pom.xml file as follows. Replace line 38:
${glassfish.home.prefix}/glassfish4
to your GlassFish home, I run GlassFish 4.1.1 installed in C:\ drive. So my modification is:
${glassfish.home.prefix}glassfish-4.1.1.
Please note the slash before glassfish was removed too. The slash after c: in line 51 suffice.
Build it again. Should be fine.
I resolved this error adding in pom.xml those lines:
<profiles>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<glassfish.home>C://Program Files//glassfish-4.1.1</glassfish.home>
</properties>
</profile>
</profiles>
I hope this help
I also usually install my glassfish development server under c:/opt so I had the same problem with the hello2 tutorial from the Java Platform, Enterprise Edition (Java EE) 8 Tutorial.
To solve this problem, I changed the "tutorial-examples/pom.xml" file which is the root parent of all the tutorial POM files. As I am using the J2EE8 tutorial, it is expected that glassfish5 is installed so this is reflected in the properties section:
<glassfish.home>${glassfish.home.prefix}/glassfish5</glassfish.home>
I not sure what tutorial version you are going through but as I see, it expects a glassfish 4 installation thus you need to edit the POM file accordingly.
Also (in the J2EE 8 tutorial), ${glassfish.home.prefix} is defined for every supported profile. For windows the windows profile, I had to change the line:
<glassfish.home.prefix>c:/</glassfish.home.prefix>
to
<glassfish.home.prefix>c:/opt</glassfish.home.prefix>
So the complete windows profile will be:
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<glassfish.home.prefix>c:/opt</glassfish.home.prefix>
<glassfish.executables.suffix>.bat</glassfish.executables.suffix>
</properties>
</profile>
Of course, you'll have to edit the POM file accordingly to your own setup.

Force Maven to use System's javac

I have made some code changes to javac and would like maven to use the version of javac that I changed. Unfortunately, it appears that maven ships its own javac implementation. How can I get maven to compile sources using the system wide javac (the one that is executed when running javac in the shell).
Without really knowing what these options mean, I tried providing the fork, and forceJavacCompilerUse which I found here: http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html
But they do not appear to help at all.
You're in the right ballpark. Look here:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html
To avoid hard-coding a filesystem path for the executable, you can use
a property. For example:
<executable>${JAVA_1_4_HOME}/bin/javac</executable>
Each developer then defines this property in settings.xml, or sets an
environment variable, so that the build remains portable.
<settings>
[...]
<profiles>
[...]
<profile>
<id>compiler</id>
<properties>
<JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>
</properties>
</profile>
</profiles>
[...]
<activeProfiles>
<activeProfile>compiler</activeProfile>
</activeProfiles>
</settings>
If you build with a different JDK, you may want to customize the jar
file manifest.
I believe the key is setting an explicit <executable>. And I suspect it can probably be hard-coded, without messing with environment variables or other clauses.
See also this link, and look at the "executable" in the sample pom.xml:
Where is the JDK version to be used by Maven compiler specified?
I believe maven looks for JVA_HOME for finding the java. So when you set it to your specific java maven should be able to use it

Resources