#StackBounty: #java #spring #grails #groovy How does class gets injected which implements interface method being called in Java or Groo…

Bounty: 250

I have below working groovy code from Spring framework project:

import org.springframework.oxm.Unmarshaller

public class ItemSearchService  {
    Unmarshaller unmarshaller;
public ItemSearchResponse getObject(InputStream xml) {
    
    ItemSearchResponse its = null;
    try {
        its = (ItemSearchResponse) unmarshaller.unmarshal(new StreamSource(xml));
    } finally {
        }
    return its;
}
}

Unmarshaller.unmarshall is actually interface method:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/oxm/Unmarshaller.html

Unmarshaller interface is implemented by several classes.

Who decides which implementing class to inject during runtime and how it decides which class to use?

Does Spring IoC mechanism does this? If so does it pick up one specific implementing class during build time when jar is generated or it does it during run time?

Also how to know which implementing class it actually used?

Will above code work outside of Spring in ordinary Java file assuming dependent jars in classpath?


Get this bounty!!!

#StackBounty: #jenkins #testing #groovy #nunit #invoke Exception has been thrown by the target of an invocation, in jenkins' groovy…

Bounty: 50

I need to maintain a jenkins groovy script (we are not sure however if it ever worked).The script for a pipeline build has multiple stages (‘revision’ and ‘build’ stages before ‘unit tests’ have succeeded), one of which is the following:

enter image description here

stage('Unit Tests')
        {
            stages
            {
                stage('Tests')
                {
                    parallel
                    {
                        stage('TRKengineTest')
                        {
                            steps 
                            {
                                bat '"C:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe" TRKengine\TRKengine.Test\bin\Release\netcoreapp3.1\TRKengine.Test.dll --result=TRKengine.Test.xml;format=nunit2 --timeout=180000  --workers=1'
                                echo "Something to do?"
                            }
                        }
                        stage('TRKengineGUItest')
                        {
                            steps 
                            {
                                //bat '"C:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe" TRKengine\TRKengineGUI.Test\bin\Release\netcoreapp3.1\TRKengineGUI.Test.dll --result=TRKengineGUI.Test.xml;format=nunit2 --timeout=180000 -v --workers=1'
                                echo "Nothing to do"
                            }
                        }
                    }
                }
                stage('Testresults')
                {
                    steps
                    {
                        nunit testResultsPattern: 'TRKengine.Test.xml, TRKengineGUI.Test.xml'
                    }
                }
            }
        }

The bat command returns a -100 error. More precisely:

C:JenkinsworkspaceTRKengine_trunk>"C:Program Files (x86)NUnit.orgnunit-consolenunit3-console.exe" TRKengineTRKengine.TestbinReleasenetcoreapp3.1TRKengine.Test.dll --result=TRKengine.Test.xml;format=nunit2 --timeout=180000 -v  --workers=1 

NUnit Console Runner 3.6.1 

Copyright (C) 2017 Charlie Poole



Runtime Environment

   OS Version: Microsoft Windows NT 10.0.14393.0

  CLR Version: 4.0.30319.42000



Test Files

    TRKengineTRKengine.TestbinReleasenetcoreapp3.1TRKengine.Test.dll





Errors, Failures and Warnings



1) Error : 

Exception has been thrown by the target of an invocation.



Server stack trace: 

   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)

   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)

   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)

   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)

   at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)

   at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)

   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)

   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)

   at NUnit.Engine.Drivers.NUnit3FrameworkDriver.Load(String testAssemblyPath, IDictionary`2 settings)

   at NUnit.Engine.Runners.DirectTestRunner.LoadPackage()

   at NUnit.Engine.Runners.DirectTestRunner.EnsurePackageIsLoaded()

   at NUnit.Engine.Runners.DirectTestRunner.RunTests(ITestEventListener listener, TestFilter filter)

   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)

   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)



Exception rethrown at [0]: 

   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)

   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

   at NUnit.Engine.ITestEngineRunner.Run(ITestEventListener listener, TestFilter filter)

   at NUnit.Engine.Runners.ProcessRunner.RunTests(ITestEventListener listener, TestFilter filter)



Test Run Summary

  Overall result: Failed

  Test Count: 0, Passed: 0, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0

  Start time: 2020-07-15 11:32:42Z

    End time: 2020-07-15 11:32:43Z

    Duration: 0.556 seconds



Results (nunit2) saved as TRKengine.Test.xml

script returned exit code -100

I have tried a couple of things:

1)First of all, it seems like the path of nunit-console.exe is wrong but as I found out, it can be used in recent releases of nunit.

2)The path of the test.dll is correct, as I manually navigated there and ..netcoreapp3.1TRKengine.Test.dll is indeed there.

3)I have spotted –result=TRKengine.Test.xml;format=nunit2 in other exampels across the internet.

4)I experimented with removing any combinations of the remaining parameters, workers/timeout -v etc, without success.

So the line where the error appears, seems to be correct, and my intuition is that probably the mistake has already happened somewhere else.

I am not sure what "invocation" means. I have also checked How do you run NUnit tests from Jenkins? that seems related and it feels like Winston33 ‘s proposition is exactly like what is happening here.

Something that is a bit worrying is that my machine is 64 bits but the path is ..x86.

I also have access to code and all projects inside the solution build successfully

I am a total beginner in both jenkins and groovy, so if I had not disclosed any necessary information please notify me.

Any help would be greatly appreciated.


Get this bounty!!!

#StackBounty: #java #maven #docker #jenkins #groovy How to automate Maven and Java JDK8 installation with groovy for Jenkins?

Bounty: 50

I’m building a Jenkins Docker image and i will like to automate the installation of Maven 3 and Java 8 last JDK. But unfortunately i I use these two groovy files locate into the groovy folder:

groovy/java.groovy:

import jenkins.model.*
import hudson.model.*
import hudson.tools.*

def inst = Jenkins.getInstance()

def desc = inst.getDescriptor("hudson.model.JDK")

def versions = [ "jdk8": "jdk-8u202"]
def installations = [];

for (v in versions) {
  def installer = new JDKInstaller(v.value, true)
  def installerProps = new InstallSourceProperty([installer])
  def installation = new JDK(v.key, "", [installerProps])
  installations.push(installation)
}

desc.setInstallations(installations.toArray(new JDK[0]))

desc.save()

groovy/maven.groovy:

import jenkins.*;
import jenkins.model.*;
import hudson.*;
import hudson.model.*;

mavenName = "maven3"
mavenVersion = "3.6.0"
println("Checking Maven installations...")

// Grab the Maven "task" (which is the plugin handle).
mavenPlugin = Jenkins.instance.getExtensionList(hudson.tasks.Maven.DescriptorImpl.class)[0]

// Check for a matching installation.
maven3Install = mavenPlugin.installations.find {
   install -> install.name.equals(mavenName)
}

// If no match was found, add an installation.
if(maven3Install == null) {
   println("No Maven install found. Adding...")

   newMavenInstall = new hudson.tasks.Maven.MavenInstallation('maven3', null,
    [new hudson.tools.InstallSourceProperty([new hudson.tasks.Maven.MavenInstaller(mavenVersion)])]
)

   mavenPlugin.installations += newMavenInstall
   mavenPlugin.save()

   println("Maven install added.")
} else {
   println("Maven install found. Done.")
}

and then i run the command:

docker run -p 8080:8080 -v `pwd`/groovy:/var/jenkins_home/jobs --rm --name jenkinsdocker jenkinsdocker:latest

Unfortunately this return an error:

java.io.IOException: jenkins.model.InvalidBuildsDir: ${ITEM_ROOTDIR}/builds does not exist and probably cannot be created

I’m just wondering if the groovy files are wrong or if there are something else.

How can i automate the maven/java installation for Jenkins while running a docker run? or is there another way to do it?


Get this bounty!!!