Class: MavenCentralPublishTool

Inherits:
Object
  • Object
show all
Defined in:
lib/mcrt.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject



39
40
41
# File 'lib/mcrt.rb', line 39

def password
  @password || (raise 'Password not yet specified')
end

#user_agentObject



45
46
47
# File 'lib/mcrt.rb', line 45

def user_agent
  @user_agent || "Ruby-#{RUBY_VERSION}"
end

#usernameObject



33
34
35
# File 'lib/mcrt.rb', line 33

def username
  @username || (raise 'Username not yet specified')
end

Class Method Details

.buildr_release(project, profile_name, username, password) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mcrt.rb', line 7

def self.buildr_release(project, profile_name, username, password)
  release_to_url = Buildr.repositories.release_to[:url]
  release_to_username = Buildr.repositories.release_to[:username]
  release_to_password = Buildr.repositories.release_to[:password]

  begin
    Buildr.repositories.release_to[:url] = 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
    Buildr.repositories.release_to[:username] = username
    Buildr.repositories.release_to[:password] = password

    project.task(':upload').invoke

    r = MavenCentralPublishTool.new
    r.username = username
    r.password = password
    r.user_agent = "Buildr-#{Buildr::VERSION}"
    r.release_sole_auto_staging(profile_name)
  ensure
    Buildr.repositories.release_to[:url] = release_to_url
    Buildr.repositories.release_to[:username] = release_to_username
    Buildr.repositories.release_to[:password] = release_to_password
  end
end

Instance Method Details

#close_repository(repository_id, description) ⇒ Object



60
61
62
63
# File 'lib/mcrt.rb', line 60

def close_repository(repository_id, description)
  post_request('https://oss.sonatype.org/service/local/staging/bulk/close',
               JSON.pretty_generate('data' => { 'description' => description, 'stagedRepositoryIds' => [repository_id] }))
end

#drop_repository(repository_id, description) ⇒ Object



72
73
74
75
# File 'lib/mcrt.rb', line 72

def drop_repository(repository_id, description)
  post_request('https://oss.sonatype.org/service/local/staging/bulk/drop',
               JSON.pretty_generate('data' => { 'description' => description, 'stagedRepositoryIds' => [repository_id] }))
end

#get_staging_repositories(profile_name, ignore_transitioning_repositories = true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/mcrt.rb', line 49

def get_staging_repositories(profile_name, ignore_transitioning_repositories = true)
  result = get_request('https://oss.sonatype.org/service/local/staging/profile_repositories')
  result = JSON.parse(result)
  result['data'].select do |repo|
    repo['profileName'] == profile_name &&
      repo['userId'] == self.username &&
      repo['userAgent'] == self.user_agent &&
      (!ignore_transitioning_repositories || !repo['transitioning'])
  end
end

#promote_repository(repository_id, description) ⇒ Object



65
66
67
68
69
70
# File 'lib/mcrt.rb', line 65

def promote_repository(repository_id, description)
  post_request('https://oss.sonatype.org/service/local/staging/bulk/promote',
               JSON.pretty_generate('data' => { 'autoDropAfterRelease' => true,
                                                'description' => description,
                                                'stagedRepositoryIds' => [repository_id] }))
end

#release_sole_auto_staging(profile_name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mcrt.rb', line 77

def release_sole_auto_staging(profile_name)
  candidates = get_staging_repositories(profile_name)
  if candidates.empty?
    raise 'Release process unable to find any staging repositories.'
  elsif 1 != candidates.size
    raise 'Release process found multiple staging repositories that could be the release just uploaded. Please visit the website https://oss.sonatype.org/index.html#stagingRepositories and manually complete the release.'
  else
    candidate = candidates[0]
    puts "Requesting close of staging repository #{profile_name}:#{candidate['repositoryId']}"
    begin
      close_repository(candidate['repositoryId'], "Closing repository for #{profile_name}")
    rescue Exception => e
      puts "#{e.class.name}: #{e.message}"
      puts e.backtrace.join("\n")
      raise 'Failed to close repository. It is likely that the release does not conform to Maven Central release requirements. Please visit the website https://oss.sonatype.org/index.html#stagingRepositories and manually complete the release.'
    end
    while get_staging_repositories(profile_name).size == 0
      puts 'Waiting for repository to close...'
      sleep 1
    end
    puts "Requesting promotion of staging repository #{profile_name}:#{candidate['repositoryId']}"
    begin
      promote_repository(candidate['repositoryId'], "Promoting repository for #{profile_name}")
    rescue Exception => e
      puts "#{e.class.name}: #{e.message}"
      puts e.backtrace.join("\n")
      raise 'Failed to promote repository. Please visit the website https://oss.sonatype.org/index.html#stagingRepositories and manually complete the release.'
    end
    repositories = get_staging_repositories(profile_name, false)
    while repositories.size == 1
      puts "Waiting for repository to be promoted..."
      sleep 1
      if repositories[0][:notifications] != 0
        raise 'Failed to promote repository. Please visit the website https://oss.sonatype.org/index.html#stagingRepositories and manually complete the release.'
      end
      repositories = get_staging_repositories(profile_name, false)
    end
  end
end