Module: NexusCli::ArtifactActions

Included in:
OSSRemote, ProRemote
Defined in:
lib/nexus_cli/mixins/artifact_actions.rb

Overview

Author:

Instance Method Summary collapse

Instance Method Details

#delete_artifact(coordinates) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/nexus_cli/mixins/artifact_actions.rb', line 82

def delete_artifact(coordinates)
  artifact = Artifact.new(coordinates)
  response = nexus.delete(nexus_url("content/repositories/#{configuration['repository']}/#{artifact.group_id.gsub(".", "/")}/#{artifact.artifact_id.gsub(".", "/")}/#{artifact.version}"))
  case response.status
  when 204
    return true
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

#get_artifact_download_url(coordinates) ⇒ String

Gets the Nexus download URL for the given [artifact].

Examples:

com.artifact:my-artifact

https://nexus.my-domain.com/content/repositories/my-repository/my-artifact/1.0.0/my-artifact-1.0.0.tgz

Parameters:

  • coordinates (String)

    the Maven identifier

Returns:

  • (String)

    The Nexus download url for the artifact



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/nexus_cli/mixins/artifact_actions.rb', line 172

def get_artifact_download_url(coordinates)
  artifact = Artifact.new(coordinates)
  query = {:g => artifact.group_id, :a => artifact.artifact_id, :e => artifact.extension, :v => artifact.version, :r => configuration['repository']}
  query.merge!({:c => artifact.classifier}) unless artifact.classifier.nil?
  response = nexus.get(nexus_url("service/local/artifact/maven/redirect"), :query => query)
  case response.status
    when 301, 307
      # Follow redirect and return download URL.
      return response.content.gsub(/If you are not automatically redirected use this url: /, "")
    when 404
      raise ArtifactNotFoundException
    else
      raise UnexpectedStatusCodeException.new(response.status)
  end
end

#get_artifact_info(coordinates) ⇒ String

Retrieves information about the given [artifact] and returns it in as a [String] of XML.

Parameters:

  • coordinates (String)

    the Maven identifier

Returns:

  • (String)

    A string of XML data about the desired artifact



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nexus_cli/mixins/artifact_actions.rb', line 100

def get_artifact_info(coordinates)
  artifact = Artifact.new(coordinates)
  query = {:g => artifact.group_id, :a => artifact.artifact_id, :e => artifact.extension, :v => artifact.version, :r => configuration['repository']}
  query.merge!({:c => artifact.classifier}) unless artifact.classifier.nil?
  response = nexus.get(nexus_url("service/local/artifact/maven/resolve"), query)
  case response.status
  when 200
    return response.content
  when 404
    raise ArtifactNotFoundException
  when 503
    raise CouldNotConnectToNexusException
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

#pull_artifact(coordinates, destination = nil) ⇒ Hash

Retrieves a file from the Nexus server using the given [String] coordinates. Optionally provide a destination [String].

Parameters:

  • coordinates (String)
  • destination (String) (defaults to: nil)

Returns:

  • (Hash)

    Some information about the artifact that was pulled.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nexus_cli/mixins/artifact_actions.rb', line 15

def pull_artifact(coordinates, destination=nil)
  artifact = Artifact.new(coordinates)

  if artifact.version.casecmp("latest")
    artifact.version = REXML::Document.new(get_artifact_info(coordinates)).elements["//version"].text
  end

  file_name = artifact.file_name
  destination = File.join(File.expand_path(destination || "."), file_name)
  query = {:g => artifact.group_id, :a => artifact.artifact_id, :e => artifact.extension, :v => artifact.version, :r => configuration['repository']}
  query.merge!({:c => artifact.classifier}) unless artifact.classifier.nil?
  response = nexus.get(nexus_url("service/local/artifact/maven/redirect"), :query => query)
  case response.status
  when 301, 307
    # Follow redirect and stream in chunks.
    artifact_file = File.open(destination, "wb") do |io|
      nexus.get(response.content.gsub(/If you are not automatically redirected use this url: /, "")) do |chunk|
        io.write(chunk)
      end
    end
  when 404
    raise ArtifactNotFoundException
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
  {
    :file_name => file_name,
    :file_path => File.expand_path(destination),
    :version   => artifact.version,
    :size      => File.size(File.expand_path(destination))
  }
end

#push_artifact(coordinates, file) ⇒ Boolean

Pushes the given [file] to the Nexus server under the given [artifact] identifier.

Parameters:

  • coordinates (String)

    the Maven identifier

  • file (type)

    the path to the file

Returns:

  • (Boolean)

    returns true when successful



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nexus_cli/mixins/artifact_actions.rb', line 55

def push_artifact(coordinates, file)
  artifact = Artifact.new(coordinates)
  put_string = "content/repositories/#{configuration['repository']}/#{artifact.group_id.gsub(".", "/")}/#{artifact.artifact_id.gsub(".", "/")}/#{artifact.version}/#{artifact.file_name}"
  response = nexus.put(nexus_url(put_string), File.open(file))

  case response.status
  when 201
    pom_name = "#{artifact.artifact_id}-#{artifact.version}.pom"
    put_string = "content/repositories/#{configuration['repository']}/#{artifact.group_id.gsub(".", "/")}/#{artifact.artifact_id.gsub(".", "/")}/#{artifact.version}/#{pom_name}"
    pom_file = generate_fake_pom(pom_name, artifact)
    nexus.put(nexus_url(put_string), File.open(pom_file))
    delete_string = "/service/local/metadata/repositories/#{configuration['repository']}/content/#{artifact.group_id.gsub(".", "/")}/#{artifact.artifact_id.gsub(".", "/")}"
    nexus.delete(nexus_url(delete_string))
    return true
  when 400
    raise BadUploadRequestException
  when 401
    raise PermissionsException
  when 403
    raise PermissionsException
  when 404
    raise NexusHTTP404.new(response.content)
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

#search_artifacts_lucene(coordinates) ⇒ String

Searches for an artifact using the lucene indexer. requires UI: Search role repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/path__lucene_search.html

Examples:

com.artifact:my-artifact:jar:4.9.*

Parameters:

  • coordinates (String)

    the Maven identifier

Returns:

  • (String)


147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/nexus_cli/mixins/artifact_actions.rb', line 147

def search_artifacts_lucene(coordinates)
  artifact = Artifact.new(coordinates)
  query = {:g => artifact.group_id, :a => artifact.artifact_id, :e => artifact.extension, :v => artifact.version, :r => configuration['repository']}
  query.merge!({:c => artifact.classifier}) unless artifact.classifier.nil?
  response = nexus.get(nexus_url("service/local/lucene/search"), query)
  case response.status
    when 200
      return response.content
    else
      raise UnexpectedStatusCodeException.new(response.status)
  end
end

#search_for_artifacts(coordinates) ⇒ Array<String>

Searches for an artifact using the given identifier.

Examples:

com.artifact:my-artifact

1.0.0     `nexus-cli pull com.artifact:my-artifact:tgz:1.0.0`
2.0.0     `nexus-cli pull com.artifact:my-artifact:tgz:2.0.0`
3.0.0     `nexus-cli pull com.artifact:my-artifact:tgz:3.0.0`

Parameters:

  • coordinates (String)

    the Maven identifier

Returns:

  • (Array<String>)

    a formatted Array of results



128
129
130
131
132
133
134
135
136
137
# File 'lib/nexus_cli/mixins/artifact_actions.rb', line 128

def search_for_artifacts(coordinates)
  group_id, artifact_id = coordinates.split(":")
  response = nexus.get(nexus_url("service/local/data_index"), :query => {:g => group_id, :a => artifact_id})
  case response.status
  when 200
    return response.content
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

#transfer_artifact(coordinates, from_repository, to_repository) ⇒ Object



160
161
162
# File 'lib/nexus_cli/mixins/artifact_actions.rb', line 160

def transfer_artifact(coordinates, from_repository, to_repository)
  do_transfer_artifact(coordinates, from_repository, to_repository)
end