title: Nexus Scripting: how to get environment variables?
body: <p>How can we access environment variables from a Groovy script uploaded on Nexus 3 repository manager?</p>
  <p>When trying to access an environment variable via Nexus script:</p>
  <pre><code>final Integer HTTP_PORT = Integer.parseInt(System.getenv(&quot;DOCKER_REGISTRY_PORT&quot;)) ?: 5050
  </code></pre>
  <p>The script fails with the following error, complaining about the <code>indirect import of System</code> :</p>
  <blockquote>
  <p>Error: b'{\n  &quot;name&quot; : &quot;nexus3-create-docker-hosted-respository&quot;,\n
  &quot;result&quot; :
  &quot;org.codehaus.groovy.control.MultipleCompilationErrorsException:
  startup failed:\nGeneral error during canonicalization: Indirect
  import checks prevents usage of
  expression\n\njava.lang.SecurityException: Indirect import checks
  prevents usage of expression\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.assertExpressionAuthorized(SecureASTCustomizer.java:779)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitMethodCallExpression(SecureASTCustomizer.java:920)\n\tat
  org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:70)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitListOfExpressions(SecureASTCustomizer.java:1105)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitTupleExpression(SecureASTCustomizer.java:994)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitArgumentlistExpression(SecureASTCustomizer.java:1159)\n\tat
  org.codehaus.groovy.ast.expr.ArgumentListExpression.visit(ArgumentListExpression.java:76)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitMethodCallExpression(SecureASTCustomizer.java:931)\n\tat
  org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:70)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitBooleanExpression(SecureASTCustomizer.java:983)\n\tat
  org.codehaus.groovy.ast.expr.BooleanExpression.visit(BooleanExpression.java:42)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitTernaryExpression(SecureASTCustomizer.java:952)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitShortTernaryExpression(SecureASTCustomizer.java:959)\n\tat
  org.codehaus.groovy.ast.expr.ElvisOperatorExpression.visit(ElvisOperatorExpression.java:60)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitBinaryExpression(SecureASTCustomizer.java:966)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitDeclarationExpression(SecureASTCustomizer.java:1095)\n\tat
  org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:89)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitExpressionStatement(SecureASTCustomizer.java:846)\n\tat
  org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:42)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.visitBlockStatement(SecureASTCustomizer.java:806)\n\tat
  org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer.call(SecureASTCustomizer.java:616)\n\tat
  org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1087)\n\tat
  org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:624)\n\tat
  org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:602)\n\tat
  org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:579)\n\tat
  groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323)\n\tat
  groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293)\n\tat
  groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:279)\n\tat
  groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:236)\n\tat
  org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:335)\n\tat
  org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:157)\n\tat org.sonatype.nexus.internal.script.ScriptServiceImpl.eval(ScriptServiceImpl.java:138)\n\tat
  org.sonatype.nexus.internal.script.ScriptServiceImpl.eval(ScriptServiceImpl.java:147)\n\tat
  org.sonatype.nexus.common.script.ScriptService$eval.call(Unknown
  Source)\n\tat
  org.sonatype.nexus.script.plugin.internal.rest.ScriptResource.run(ScriptResource.groovy:186)\n\tat
  org.sonatype.nexus.script.plugin.internal.rest.ScriptResource$$EnhancerByGuice$$890997177.GUICE$TRAMPOLINE()\n\tat
  com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:74)\n\tat
  com.palominolabs.metrics.guice.ExceptionMeteredInterceptor.invoke(ExceptionMeteredInterceptor.java:23)\n\tat
  com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:75)\n\tat
  com.palominolabs.metrics.guice.TimedInterceptor.invoke(TimedInterceptor.java:26)\n\tat
  com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:75)\n\tat
  com.google.inject.internal.InterceptorStackCallback.invoke(InterceptorStackCallback.java:55)\n\tat
  org.sonatype.nexus.script.plugin.internal.rest.ScriptResource$$EnhancerByGuice$$890997177.run()\n\tat
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat
  java.lang.reflect.Method.invoke(Method.java:498)\n\tat
  org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:138)\n\tat
  org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:546)\n\tat
  org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:435)\n\tat
  org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$0(ResourceMethodInvoker.java:396)\n\tat
  org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:358)\n\tat
  org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:398)\n\tat
  org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:365)\n\tat
  org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:338)\n\tat
  org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:440)\n\tat
  org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:229)\n\tat
  org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:135)\n\tat
  org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:358)\n\tat
  org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:138)\n\tat
  org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:215)\n\tat
  org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:245)\n\tat
  org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:61)\n\tat
  org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)\n\tat
  org.sonatype.nexus.siesta.internal.resteasy.ComponentContainerImpl.service(ComponentContainerImpl.java:111)\n\tat
  org.sonatype.nexus.siesta.SiestaServlet.service(SiestaServlet.java:137)\n\tat
  javax.servlet.http.HttpServlet.service(HttpServlet.java:790)\n\tat
  com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:290)\n\tat
  com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:280)\n\tat
  com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:184)\n\tat
  com.google.inject.servlet.DynamicServletPipeline.service(DynamicServletPipeline.java:71)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:85)\n\tat
  org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:112)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  com.codahale.metrics.servlet.AbstractInstrumentedFilter.doFilter(AbstractInstrumentedFilter.java:112)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)\n\tat
  org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)\n\tat
  org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)\n\tat
  org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)\n\tat
  org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)\n\tat
  org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)\n\tat
  org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)\n\tat
  org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)\n\tat
  org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)\n\tat
  org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)\n\tat
  org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)\n\tat
  org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)\n\tat
  org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)\n\tat
  org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:450)\n\tat
  org.sonatype.nexus.security.SecurityFilter.executeChain(SecurityFilter.java:96)\n\tat
  org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)\n\tat
  org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)\n\tat
  org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)\n\tat
  org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387)\n\tat
  org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)\n\tat
  org.sonatype.nexus.security.SecurityFilter.doFilterInternal(SecurityFilter.java:112)\n\tat
  org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)\n\tat
  org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)\n\tat
  org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  com.sonatype.nexus.licensing.internal.LicensingRedirectFilter.doFilter(LicensingRedirectFilter.java:116)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  com.codahale.metrics.servlet.AbstractInstrumentedFilter.doFilter(AbstractInstrumentedFilter.java:112)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  org.sonatype.nexus.internal.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:79)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  org.sonatype.nexus.internal.web.EnvironmentFilter.doFilter(EnvironmentFilter.java:101)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  org.sonatype.nexus.internal.web.HeaderPatternFilter.doFilter(HeaderPatternFilter.java:98)\n\tat
  com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)\n\tat
  com.google.inject.servlet.DynamicFilterPipeline.dispatch(DynamicFilterPipeline.java:104)\n\tat
  com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:133)\n\tat
  org.sonatype.nexus.bootstrap.osgi.DelegatingFilter.doFilter(DelegatingFilter.java:73)\n\tat
  org.eclipse.jetty.servlet.FilterHolder.doFilter(FilterHolder.java:201)\n\tat
  org.eclipse.jetty.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1601)\n\tat
  org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:548)\n\tat
  org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)\n\tat
  org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:602)\n\tat
  org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)\n\tat
  org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235)\n\tat
  org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624)\n\tat
  org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)\n\tat
  org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1434)\n\tat
  org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)\n\tat
  org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501)\n\tat
  org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594)\n\tat
  org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)\n\tat
  org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1349)\n\tat
  org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)\n\tat
  org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)\n\tat
  com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:239)\n\tat
  org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)\n\tat
  org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)\n\tat
  org.eclipse.jetty.server.Server.handle(Server.java:516)\n\tat
  org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:388)\n\tat
  org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:633)\n\tat
  org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:380)\n\tat
  org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277)\n\tat
  org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)\n\tat
  org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)\n\tat
  org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)\n\tat
  org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338)\n\tat
  org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315)\n\tat
  org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173)\n\tat
  org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)\n\tat
  org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:386)\n\tat
  org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883)\n\tat
  org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034)\n\tat
  java.lang.Thread.run(Thread.java:750)\nCaused by:
  java.lang.SecurityException: Importing [java.lang.System] is not
  allowed\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer.assertImportIsAllowed(SecureASTCustomizer.java:677)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer.access$800(SecureASTCustomizer.java:183)\n\tat
  org.codehaus.groovy.control.customizers.SecureASTCustomizer$SecuringCodeVisitor.assertExpressionAuthorized(SecureASTCustomizer.java:765)\n\t...
  152 more\n\n1 error\n&quot;\n}'</p>
  </blockquote>