Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public void setTimeout(Integer timeout) {
root.setIntegerValue(PROP_TIMEOUT, timeout);
}

public void setTimeoutSec(Integer timeout) {
setTimeout(timeout);
}

public Integer getTimeout() {
return root.getIntegerValue(PROP_TIMEOUT);
}
Expand Down Expand Up @@ -863,6 +867,10 @@ public Boolean isDeleteBuildArtifacts() {
return getBooleanValue(DELETE_BUILD_ARTIFACTS, true);
}

public void setBuildRetentionMaxDays(Integer daysToKeep) {
setBuildRetentionDays(daysToKeep);
}

public void setBuildRetentionDays(Integer daysToKeep) {
setIntegerValue(BUILD_RETENTION_DAYS, daysToKeep);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject
import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader
import org.codehaus.gmaven.mojo.GroovyMojo
import org.eclipse.aether.RepositorySystem
import org.eclipse.aether.impl.ArtifactDescriptorReader
import org.eclipse.aether.internal.impl.DefaultRepositorySystem
import org.gcontracts.annotations.Requires
import org.jfrog.build.api.BuildInfoProperties
import org.jfrog.build.client.ClientConfigurationFields
import org.jfrog.build.extractor.maven.BuildInfoRecorder
import org.jfrog.build.extractor.maven.BuildInfoRecorderLifecycleParticipant
import org.sonatype.aether.RepositorySystem
import org.sonatype.aether.impl.ArtifactDescriptorReader
import org.sonatype.aether.impl.internal.DefaultRepositorySystem
import java.text.SimpleDateFormat


/**
* Artifactory plugin creating and deploying JSON build data together with build artifacts.
*/
@Mojo ( name = 'extract-build-info', defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true )
class ExtractorMojo extends GroovyMojo
@Mojo ( name = 'publish', defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true )
class PublishMojo extends GroovyMojo
{
/**
* ---------------------------
Expand Down Expand Up @@ -73,7 +74,7 @@ class ExtractorMojo extends GroovyMojo
boolean pomPropertiesPriority = false

@Parameter
String deployProperties
Map<String, String> deployProperties = [:]

/**
* ----------------
Expand Down Expand Up @@ -105,7 +106,7 @@ class ExtractorMojo extends GroovyMojo
/**
* Helper object, should be initialized last (reads values of other instance fields).
*/
private final ExtractorMojoHelper helper = new ExtractorMojoHelper( this )
private final PublishMojoHelper helper = new PublishMojoHelper( this )


@SuppressWarnings([ 'GroovyAccessibility' ])
Expand All @@ -122,7 +123,7 @@ class ExtractorMojo extends GroovyMojo
if ( log.debugEnabled ){ helper.printConfigurations() }

skipDefaultDeploy()
updateBuildInfo()
completeConfig()
helper.createPropertiesFile()
overrideResolutionRepository()
recordBuildInfo()
Expand All @@ -139,15 +140,19 @@ class ExtractorMojo extends GroovyMojo


/**
* Updates {@link #buildInfo} fields.
* Completes various configuration settings.
*/
@SuppressWarnings([ 'GroovyAccessibility' ])
@Requires({ buildInfo && session && project })
private void updateBuildInfo ()
private void completeConfig ()
{
buildInfo.buildTimestamp = session.startTime.time as String
buildInfo.buildStarted = new SimpleDateFormat( 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ' ).format( session.startTime ) // 2013-06-23T18\:38\:37.597+0200
buildInfo.buildName = helper.updateValue( buildInfo.buildName ) ?: project.artifactId
buildInfo.buildNumber = helper.updateValue( buildInfo.buildNumber ) ?: buildInfo.buildTimestamp
buildInfo.buildTimestamp = session.startTime.time as String
buildInfo.buildStarted = new SimpleDateFormat( 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ' ).format( session.startTime ) // 2013-06-23T18\:38\:37.597+0200
buildInfo.buildName = helper.updateValue( buildInfo.buildName ) ?: project.artifactId
buildInfo.buildNumber = helper.updateValue( buildInfo.buildNumber ) ?: buildInfo.buildTimestamp
buildInfo.buildAgentName = 'Maven'
buildInfo.buildAgentVersion = helper.mavenVersion()
blackDuck.runChecks = blackDuck.delegate.props.keySet().any { it.startsWith( BuildInfoProperties.BUILD_INFO_BLACK_DUCK_PROPERTIES_PREFIX )}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jfrog.build.extractor.maven.plugin

import org.apache.maven.Maven
import org.jfrog.build.api.BuildInfoFields
import org.gcontracts.annotations.Ensures
import org.gcontracts.annotations.Requires
Expand All @@ -12,10 +13,10 @@ import java.lang.reflect.Method
/**
* Helper merging all mojo properties.
*/
class ExtractorMojoHelper
class PublishMojoHelper
{
@Delegate
private final ExtractorMojo mojo
private final PublishMojo mojo

private final Properties systemProperties

Expand All @@ -34,11 +35,9 @@ class ExtractorMojoHelper
( File ) : 'path/to/file',
( String ) : ' .. ' ].asImmutable()


@SuppressWarnings([ 'GrFinalVariableAccess' ])
@Requires({ mojo })
@Ensures ({ this.mojo && ( this.systemProperties != null ) && prefixPropertyHandlers })
ExtractorMojoHelper ( ExtractorMojo mojo )
PublishMojoHelper ( PublishMojo mojo )
{
this.mojo = mojo
final systemProperty = System.getProperty( BuildInfoConfigProperties.PROP_PROPS_FILE )
Expand All @@ -50,6 +49,24 @@ class ExtractorMojoHelper
}


/**
* Retrieves current Maven version.
*/
@Ensures ({ result })
String mavenVersion()
{
final resourceLocation = 'META-INF/maven/org.apache.maven/maven-core/pom.properties'
final resourceStream = Maven.classLoader.getResourceAsStream( resourceLocation )
assert resourceStream, "Failed to load '$resourceLocation'"

final properties = new Properties()
properties.load( resourceStream )
resourceStream.close()

properties[ 'version' ] ?: 'Unknown'
}


/**
* Prints out all possible Mojo <configuration> settings.
*/
Expand Down Expand Up @@ -121,10 +138,10 @@ class ExtractorMojoHelper
assert prefixPropertyHandlers.values().each { assert it.delegate?.props && it.delegate.props.is( artifactory.delegate.root.props ) }

final mergedProperties = new Properties()
final deployProperties = ( [ ( BuildInfoFields.BUILD_TIMESTAMP ) : buildInfo.buildTimestamp,
( BuildInfoFields.BUILD_NAME ) : buildInfo.buildName,
( BuildInfoFields.BUILD_NUMBER ) : buildInfo.buildNumber ] +
readProperties( deployProperties )).collectEntries {
final deployProperties = ([ ( BuildInfoFields.BUILD_TIMESTAMP ) : buildInfo.buildTimestamp,
( BuildInfoFields.BUILD_NAME ) : buildInfo.buildName,
( BuildInfoFields.BUILD_NUMBER ) : buildInfo.buildNumber ] +
deployProperties ).collectEntries {
String key, String value -> [ "${ ClientProperties.PROP_DEPLOY_PARAM_PROP_PREFIX }${ key }".toString(), value ]
}

Expand Down Expand Up @@ -188,20 +205,26 @@ class ExtractorMojoHelper
String updateValue( String value )
{
if ( ! value?.with{ contains( '{' ) && contains( '}' ) }){ return value?.trim() }
final isQuoted = { String s -> s.with { startsWith( '"' ) && endsWith( '"' ) }}
final unquote = { String s -> s.substring( 1, s.size() - 1 )}
final result = value.trim().replaceAll( /\{([^}]*)\}/ ){

final String expression = it[ 1 ]

value.trim().replaceAll( /(\$?\{)([^}]+)(\})/ ){
if ( isQuoted( expression )) { return "{${ unquote( expression )}}" }

final originalExpression = "${ it[ 1 ] }${ it[ 2 ] }${ it[ 3 ] }"
final expressions = (( String ) it[ 2 ] ).tokenize( '|' )*.trim().grep()
final expressions = expression.tokenize( '|' )*.trim().grep()

assert originalExpression, "Unexpected original expression of '$value' - '$originalExpression'"
assert expressions, "Value '$value' - no variables found in expression '$originalExpression'"
if ( ! expressions ){ return null }

final variables = (( expressions.size() == 1 ) ? expressions : expressions[ 0 .. -2 ] )
final defaultValue = (( expressions.size() == 1 ) ? null : expressions[ -1 ] )
final result = variables.collect { System.getenv( it ) ?: System.getProperty( it )}.grep()[ 0 ] ?: defaultValue
final lastValue = expressions[ -1 ]
final defaultValue = isQuoted( lastValue ) ? unquote( lastValue ) : null
final variables = ( defaultValue != null ) ? (( expressions.size() > 1 ) ? expressions[ 0 .. -2 ] : [] ) :
expressions

result ?: originalExpression
variables.collect { System.getenv( it ) ?: System.getProperty( it )}.grep()[ 0 ] ?: defaultValue
}

( result == 'null' ) ? null : result
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package org.jfrog.build.extractor.maven.plugin

import org.eclipse.aether.repository.RepositoryPolicy
import org.gcontracts.annotations.Ensures
import org.gcontracts.annotations.Requires
import org.sonatype.aether.RepositorySystemSession
import org.sonatype.aether.impl.ArtifactResolver
import org.sonatype.aether.repository.RemoteRepository
import org.sonatype.aether.resolution.ArtifactRequest
import org.sonatype.aether.resolution.ArtifactResolutionException
import org.sonatype.aether.resolution.ArtifactResult
import org.eclipse.aether.RepositorySystemSession
import org.eclipse.aether.impl.ArtifactResolver
import org.eclipse.aether.repository.RemoteRepository
import org.eclipse.aether.resolution.ArtifactRequest
import org.eclipse.aether.resolution.ArtifactResolutionException
import org.eclipse.aether.resolution.ArtifactResult


/**
* {@link ArtifactResolver} implementation using remote repository specified for all resolution requests.
*/
@SuppressWarnings([ 'GrFinalVariableAccess' ])
class RepositoryResolver implements ArtifactResolver
{
private final ArtifactResolver delegateResolver
private final List<RemoteRepository> remoteRepositories


@SuppressWarnings([ 'JavaStylePropertiesInvocation', 'GroovySetterCallCanBePropertyAccess' ])
@Requires({ delegateResolver && remoteRepository })
@Ensures ({ this.delegateResolver && this.remoteRepositories })
RepositoryResolver ( ArtifactResolver delegateResolver, String remoteRepository )
{
final repository = new RemoteRepository( remoteRepository, 'default', remoteRepository )
repository.setPolicy( true, null )
repository.setPolicy( false, null )
final repository = new RemoteRepository.Builder( remoteRepository, 'default', remoteRepository ).
setReleasePolicy ( new RepositoryPolicy( true, null, null )).
setSnapshotPolicy( new RepositoryPolicy( true, null, null )).
build()

this.delegateResolver = delegateResolver
this.remoteRepositories = [ repository ].asImmutable()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component-set>
<components>
<component>
<role>
org.apache.maven.lifecycle.mapping.LifecycleMapping
</role>
<role-hint>jar</role-hint>
<implementation>
org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
</implementation>
<configuration>
<phases>
<deploy>
org.jfrog.buildinfo:artifactory-maven-plugin:publish
</deploy>
</phases>
</configuration>
</component>
</components>
</component-set>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.jfrog.build.extractor.maven.plugin

import spock.lang.Specification
import spock.lang.Unroll


/**
* {@link PublishMojoHelper} specs.
*/
class PublishMojoHelperSpec extends Specification
{
final helper = new PublishMojoHelper( new PublishMojo())


@Unroll( "'#expression' => '#expected'" )
def 'updateValue() - constants' ( String expression, String expected )
{
expect:
helper.updateValue( expression ) == expected

where:
expression | expected
'{}' | null
'{""}' | '{}'
'{"abc"}' | '{abc}'
'{abc}' | null
'{"abc|def"}' | '{abc|def}'
'{}{""}' | 'null{}'
'{""}{}' | '{}null'
'{"abc"}def{""}' | '{abc}def{}'
'{"abc"}def{"zzz"}' | '{abc}def{zzz}'
'{"abc"}def{"zzz|uuu"}' | '{abc}def{zzz|uuu}'
'{"abc|xxx"}def{"zzz|uuu"}'| '{abc|xxx}def{zzz|uuu}'
'{A|B|C|"def"}' | 'def'
'{A|B|C|def}' | null
'{A|B}_{D|E}' | 'null_null'
'{A|B}_{D|E|"f"}' | 'null_f'
'{A|B|"c"}_{D|E}' | 'c_null'
'{A|B|"c"}_{D|E|"ee"}' | 'c_ee'
'{JAVA_HOME2|EDITOR2}' | null
'{JAVA_HOME2|EDITOR2|""}' | ''
'{JAVA_HOME2|EDITOR2|"a"}' | 'a'
'{A|EDITOR2|B|"aa"}' | 'aa'
}


@Unroll( "'#expression' => System.getenv(#variables)" )
def 'updateValue() - variables' ( String expression, List<String> variables )
{
expect:
( System.getenv( 'JAVA_HOME' ) == null ) || ( helper.updateValue( expression ) == variables.collect { System.getenv( it ) }.join( '|' ))

where:
expression | variables
'{JAVA_HOME}' | [ 'JAVA_HOME' ]
'{A|JAVA_HOME|B}' | [ 'JAVA_HOME' ]
'{JAVA_HOME|JAVA_HOME}' | [ 'JAVA_HOME' ]
'{AAA|JAVA_HOME}' | [ 'JAVA_HOME' ]
'{JAVA_HOME|BBB}' | [ 'JAVA_HOME' ]
'{JAVA_HOME}|{JAVA_HOME}' | [ 'JAVA_HOME', 'JAVA_HOME' ]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ public void sessionStarted(ExecutionEvent event) {
}
catch ( Throwable t )
{
logger.error( "'sessionStarted' listener has failed: ", t );
throw new RuntimeException( "'sessionStarted' listener has failed: ", t );
String message = getClass().getName() + ".sessionStarted() listener has failed: ";
logger.error( message, t );
throw new RuntimeException( message, t );
}
}

Expand All @@ -147,8 +148,9 @@ public void sessionEnded(ExecutionEvent event) {
}
catch ( Throwable t )
{
logger.error( "'sessionEnded' listener has failed: ", t );
throw new RuntimeException( "'sessionEnded' listener has failed: ", t );
String message = getClass().getName() + ".sessionEnded() listener has failed: ";
logger.error( message, t );
throw new RuntimeException( message, t );
}
finally {
String propertyFilePath = System.getenv(BuildInfoConfigProperties.PROP_PROPS_FILE);
Expand Down
Loading