jvm able to access with eclipse but not with jenkins - java

I have issue with jvm argument being passed:
1. I can pass jvm argument in eclipse as -Dname=value and access the same through java as system.getproperty("name");
2. But when i try the same with Jenkins, as it is a maven project with multiple pom, not able to pass this param i.e., it shows null on printing it.
Done with lot lot more surfing and tries... but didnt get a fix yet :(
passed param in mvn .. -Dname=value ...
tried with setting property in pom etc.., nothing helped
Is ther any other way to set system property in global access so that all the pom and java files can access it.
MAVAN_OPTS, jenkins ettings nothing worked...

You can set system variables via the <systemPropertyVariables> configuration of maven surfire plugin.
See documentation and full example here:
https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

Related

configure javadoc links in offline mode to link to local copy

How do I create javadocs that link to files in the local filesystem in offline mode?
I am building javadocs for a java package and want the links of external packages (including java.lang) to point to a local copy. This works by setting the links property like so:
<configuration>
<links>
<link>/usr/share/doc/default-jdk-doc/api/</link>
</links>
</configuration>
However, the links property is ignored when working offline, even when
linking to a local copy of the javadocs. My understanding is that in such case, I should be using offlineLinks but I can't make it work.
All documentation I see for offlineLinks sets url to an external URL and location to a local file. The use case seems to be creating links to external server, by using a local copy. I have tried to set both url and location to the same value but this seems to be ignored.
<offlineLinks>
<offlineLink>
<url>/usr/share/doc/default-jdk-doc/api/</url>
<location>/usr/share/doc/default-jdk-doc/api/</location>
</offlineLink>
</offlineLinks>
I have created a minimal example as a github gist which I am calling like so:
mvn --offline javadoc:javadoc
It has a single package and method that takes a String as argument. I want the javadocs to create a link to a local copy of the docs but instead I keep getting String linked to docs.oracle.com instead.
The javadoc program would handle the -links option properly, even if it was a local directory and there was no without internet connection. The problem is the maven javadoc plugin which ignores the links property in offline mode. The solution is to pass that option directly to the javadoc program via the additionalparam property like so:
<configuration>
<additionalparam>-link /usr/share/doc/default-jdk-doc/api/</additionalparam>
</configuration>
This has two problems:
It becomes too long if linking to many different packages. In theory, one should be able to pass multiple -link options in a single additionalparam parameter.
The additionalparam is deprecated. However, there is no other maven property to pass javadoc options directly so hopefully they won't actually remove it.
The offlineLinks property, which would map to the command line option -linkoffline seems to really require an external URL. I am unsure if that's a requirement in the javadoc program or in the maven javadoc plugin.

maven-surefire-plugin system properties and eclipse integration?

I set system properties for the surefire execution in maven's pom.xml. Eclipse ignores those. Is there any way to fix that issue?
In particular I use the basedir property to determine file locations, ie the build directory etc.
I don't want to run the tests as "maven test" because that's slow.
Update
I'm now using the test class' resource location to determine the project location on disk:
https://github.com/jjYBdx4IL/misc/blob/master/env-utils/src/main/java/com/github/jjYBdx4IL/utils/env/Maven.java#L64
This requires no configuration at all.
<dependency>
<groupId>com.github.jjYBdx4IL.utils</groupId>
<artifactId>env-utils</artifactId>
<version>1.0</version>
</dependency>
I'm assuming from what you're saying, when you run maven at the command line, you've got environment variables set, which maven picks up. If this is the case, then either passing it along on the command line or setting the system properties seem like it might be the solution that you're looking for. The other thing is, make sure you're using "project.basedir", not just "basedir". See
here for the property names

TeamCity :: How can I access teamcity build ID in Java

Teamcity Build ID (which is different from BUILD_NUMBER) is used in various URLs. I want to send an email having path of a build's artifacts/ overview etc.
In Java, we can get currently running teamcity build number as follows:
String tc_BuildNumber = System.getenv("BUILD_NUMBER");
This is because TC provides an environment variable namely BUILD_NUMBER.
But unfortunately, there is no environment variable corresponding to BUILD_ID.
TeamCity does provide Configuration parameters (like teamcity.build.id) and System property (like system.teamcity.auth.userId) but I don't know how to access these using Java. I want to read the value of teamCity.build.id jusy like we can read environment variables names mentioned in How to fetch the Value of Teamcity Configuration in java?
Are you executing the java code using a build runner?
If so, then you should be able to pass %system.teamcity.build.id% to the runner, and make it available to your code.
i.e. If you're using the command line runner
java -Dbuild_id=%system.teamcity.build.id%
which you can then access as system arguments
Or if you're using gradle, you can do something like
if (project.hasProperty("teamcity")) {
version = project.teamcity["teamcity.build.id"]
}
and pass 'version' to the java command line.
In maven, you can just access it using:
${teamcity.build.id}
in your pom.xml
(I could do with a little more info about how you're running java to answer this specifically)
I've noticed that lots of people want to know the answer to this question.
Fortunately with the help of comment from #Jayan I was able to do solve my exact problem which was how to get URL for build artifacts.
As mentioned in link https://confluence.jetbrains.com/display/TCD10/Patterns+For+Accessing+Build+Artifacts, by default, TeamCity uses Internal Build ID for the path that can be used to access build artifacts:
/repository/download/BUILD_TYPE_EXT_ID/BUILD_ID:id/ARTIFACT_PATH
Accessing build Id could be difficult in the runtime(That is the reason of this question), but we can also use Build Number to access artifacts
/repository/download/BUILD_TYPE_EXT_ID/BUILD_NUMBER/ARTIFACT_PATH
And as shown in my question build number can be accessed as
String BUILD_NUMBER= System.getenv("BUILD_NUMBER");
and
String BUILD_TYPE_EXT_ID = System.getenv("TEAMCITY_BUILDCONF_NAME");
Yes, but you can create env var with value "%system.teamcity.buildType.id%" and read it in build. After that you can do an api request like:
$APIURL = "${API_BaseUrl}/httpAuth/app/rest/builds/?locator=buildType:${API_BuildType},state:running,count:1"
$APIXML = (Invoke-RestMethod -Headers $API_CredentialsHeader -Credential $API_Credentials -Uri $APIURL -Method GET -ContentType "application/xml" -TimeoutSec 20)
# Here you build id.
$APIXML.builds.build.id
This is PS example. But idea the same. In Java that might be more easy.
A link to a TeamCity build can use build number instead of buildID. But, it requires buildTypeId as well (can be seen in build configuration page URL).
A sample of such link is:
https://buildserver/viewLog.html?buildTypeId=Project_Trunk&buildNumber=46523
Hope this helps someone.

Java not finding environment variables defined in Jenkins

I have a snippet of code that is obtaining an environment variable as follows: System.getenv("MY_VAL")
locally on my windows machine this works fine.
However, on my Jenkins CI Server which is running CentOS I am encountering some issues
I have tried setting the value of MY_VAL through both the envinject plugin as well as the global jenkins settings
If i do a pre-build step to echo the value out, it works fine, however inside my java code this is not being resolved.
How do I get this to be resolved?
You can achieve that by installing EnvInject plugin.
1) After installing check the Prepare an environment for the job option in the job configuration screen. This option will display several field for you to fill.
UPDATE
2) Fill the Script Content area with a command touch env.properties to create the file.
3) Fill the Properties Contentt field with the variables you want to inject inside your recently created env.properties file by doing so. Place one variable per line ex:
VARIABLE1=value
VARIABLE2=value
4) Reference env.properties file you've just created in the file path area.
5) At runtime Jenkins will inject those variables and they will be available to your program.
You need to inject this variable into property.file and then access this from property file. e.g. in execute shell you can define "echo MY_VAL=default > property.file" .. Later in subsequent jobs you can pass through using "Jenkins Parameterized Trigger plugin" where you have option to access parameters from property file.

How to buld a Maven project with a specified java home property in eclipse?

I want to build a maven integrated project with a given java home property. At exactly i want to write a code in java that sets the java home property for an InvocationRequest object. The main goal is to build a project with the runtime given(by an algorithm) java home. So i would call getInvoker().execute(request); to execute maven goals where request is an InvocationRequest object.
I tried to set the request java.home property with properties.setProperty(Goals.JAVA_HOME, javaHomePath); and call the method executeGoals(pom, new String[] { Goals.INSTALL, Goals.CLEAN }, properties); . This executeGoals(...) method contains getInvoker().execute(request) call and the request object definition too.
Output is: Missing: 1) com.sun:tools:jar:1.5.0 #Solved
EDIT: solved the output problem, but a new one appeared:
class file has wrong version 50.0, should be 49.0. Maybe i changed the jre home, so i think i'm compiling with a newer version of java than i'm running with.
Reminder: i want to build with a specified java home property = i want to change the compiler java home(or version) to the specified one. (In eclipse)
I would appreciate any help.
I dont think it is configurable. It is part of the Maven Core to use the JAVA_HOME environment variable. Please see the accepted answer of:
How to set specific java version to Maven
Also it is not possible to set or change environment variables (not system properties) within a Java process (for the current process). If you create another process from within your Java process, there will be methods to specify environment variables for this sub process.
Maybe the solution will be to execute a Maven command e.g. "mvn clean install" with a specific JAVA_HOME variable set as sub process (this requires, that Maven is installed and mvn is available as command). Use the ProcessBuilder to switch into the working directory, where the pom.xml of the target project is located and set the appropriate environment variable(s) before starting the process.
If Maven should not be installed at the enviroment your application is running on, you could also distribute a Maven installation with your application (maybe in a separate directory). Then you could run against the mvn.bat or mvn.sh of this distribution (depending on the os).
When using Eclipse, Build in Run as Configuration, go to the Environment tab and add the new JAVA_HOME variable.
Do not forget to check the Replace native environment ..... option.
This will override your default OS variable. No need to change at the OS level.

Categories