1212import java .io .UnsupportedEncodingException ;
1313import java .net .URI ;
1414import java .net .URLDecoder ;
15+ import java .util .ArrayList ;
1516import java .util .Arrays ;
1617import java .util .Collection ;
1718import java .util .Collections ;
99100@ BndPlugin (name = "MavenBndRepository" , parameters = Configuration .class )
100101public class MavenBndRepository extends BaseRepository implements RepositoryPlugin , RegistryPlugin , Plugin , Closeable ,
101102 Refreshable , Actionable , ToDependencyPom , ReleaseBracketingPlugin {
102- final static Pattern PREPROCESS_P = Pattern .compile ("\\ {\\ s*(?<core>[^}]+)\\ s*\\ }" );
103103
104- private final static Logger logger = LoggerFactory .getLogger (MavenBndRepository .class );
105- private static final int DEFAULT_POLL_TIME = 5 ;
104+ public static final String SONATYPE_RELEASE_DIR = "cnf/cache/sonatype-release" ;
105+ public static final String SONATYPE_SNAPSHOT_DIR = "cnf/cache/sonatype-snapshot" ;
106+ public static final String SONATYPE_DEPLOYMENTID_FILE = "deploymendID.txt" ;
106107
107- private static final String NONE = "NONE" ;
108- private static final String MAVEN_REPO_LOCAL = System .getProperty ("maven.repo.local" ,
108+ final static Pattern PREPROCESS_P = Pattern
109+ .compile ("\\ {\\ s*(?<core>[^}]+)\\ s*\\ }" );
110+
111+ private final static Logger logger = LoggerFactory
112+ .getLogger (MavenBndRepository .class );
113+ private static final int DEFAULT_POLL_TIME = 5 ;
114+
115+ private static final String NONE = "NONE" ;
116+ private static final String MAVEN_REPO_LOCAL = System .getProperty ("maven.repo.local" ,
109117 "~/.m2/repository" );
110118 private Configuration configuration ;
111119 private Registry registry ;
@@ -115,16 +123,16 @@ public class MavenBndRepository extends BaseRepository implements RepositoryPlug
115123 private boolean inited ;
116124 IndexFile index ;
117125 private ScheduledFuture <?> indexPoller ;
118- private RepoActions actions = new RepoActions (this );
126+ private RepoActions actions = new RepoActions (this );
119127 private String name ;
120128 private HttpClient client ;
121- private ReleasePluginImpl releasePlugin = new ReleasePluginImpl (this , null );
122- private File base = IO .work ;
123- private String status = null ;
129+ private ReleasePluginImpl releasePlugin = new ReleasePluginImpl (this , null );
130+ private File base = IO .work ;
131+ private String status = null ;
124132 private boolean remote ;
125- private final AtomicReference <Throwable > open = new AtomicReference <>();
133+ private final AtomicReference <Throwable > open = new AtomicReference <>();
126134 Optional <Workspace > workspace ;
127- private AtomicBoolean polling = new AtomicBoolean (false );
135+ private AtomicBoolean polling = new AtomicBoolean (false );
128136
129137 /**
130138 * Put result
@@ -282,8 +290,7 @@ private void doExtra(PutOptions options, ReleaseDTO instructions, IPom pom, Rele
282290 String clazz = extra .clazz ;
283291 File file = new File (path );
284292 if (!file .isFile ())
285- reporter .error ("-release-maven archive contains a path to a file that does not exist: %s" ,
286- file );
293+ reporter .error ("-release-maven archive contains a path to a file that does not exist: %s" , file );
287294 else {
288295 try (Resource r = new FileResource (file )) {
289296 Resource what ;
@@ -487,7 +494,6 @@ private ReleaseDTO getReleaseDTO(Processor context) {
487494 release .passphrase = sign .get ("passphrase" );
488495 }
489496
490-
491497 int clazz = 0 ;
492498
493499 for (Iterator <Entry <String , Attrs >> it = p .entrySet ()
@@ -515,8 +521,7 @@ private ReleaseDTO getReleaseDTO(Processor context) {
515521 .getAbsolutePath ();
516522 } else {
517523 reporter .warning (
518- "The -maven-release instruction has an 'archive' without the path attribute: %s" ,
519- e );
524+ "The -maven-release instruction has an 'archive' without the path attribute: %s" , e );
520525 continue ;
521526 }
522527 extra .path = path ;
@@ -649,18 +654,60 @@ synchronized boolean init() {
649654 inited = true ;
650655
651656 try {
652- List <MavenBackingRepository > release = MavenBackingRepository .create (configuration .releaseUrl (), reporter ,
653- localRepo , client );
654- List <MavenBackingRepository > snapshot = MavenBackingRepository .create (configuration .snapshotUrl (), reporter ,
655- localRepo , client );
656-
657+ List <MavenBackingRepository > release = new ArrayList <MavenBackingRepository >();
657658 MavenBackingRepository staging = null ;
658-
659- if (configuration .stagingUrl () != null ) {
660- staging = MavenBackingRepository .getBackingRepository (configuration .stagingUrl (),
661- reporter , localRepo , client );
659+ List <MavenBackingRepository > snapshot = new ArrayList <MavenBackingRepository >();
660+
661+ String releaseUrl = configuration .releaseUrl ();
662+ SonatypeMode sonatypeMode = configuration .sonatypeMode (SonatypeMode .NONE .name ());
663+
664+ String stagingUrl = configuration .stagingUrl ();
665+ String snapshotUrl = configuration .snapshotUrl ();
666+
667+ String sonatypeReleaseUrl = null ;
668+ String sonatypeSnapshotUrl = null ;
669+ switch (sonatypeMode ) {
670+ case MANUAL , AUTOPUBLISH -> {
671+ logger .info ("deployment via Sonatype Central Portal configured in {} mode" , sonatypeMode );
672+ File releaseDir = registry .getPlugin (Workspace .class )
673+ .getFile (SONATYPE_RELEASE_DIR );
674+ File snapshotDir = registry .getPlugin (Workspace .class )
675+ .getFile (SONATYPE_SNAPSHOT_DIR );
676+ if (stagingUrl == null ) {
677+ logger .debug ("deployment via relase url to Sonatype Portal configured" );
678+ List <MavenBackingRepository > releaseLocal = MavenBackingRepository .create (releaseDir .toURI ()
679+ .toString (), reporter , localRepo , client );
680+ release .addAll (releaseLocal );
681+ sonatypeReleaseUrl = releaseUrl ;
682+ } else {
683+ logger .debug ("deployment via staging url to Sonatype Portal configured" );
684+ release = MavenBackingRepository .create (releaseUrl , reporter , localRepo , client );
685+ staging = MavenBackingRepository .getBackingRepository (releaseDir .toURI ()
686+ .toString (), reporter , localRepo , client );
687+ sonatypeReleaseUrl = stagingUrl ;
688+ }
689+ if (snapshotUrl != null ) {
690+ logger .debug ("deployment via snapshot url to Sonatype Portal configured" );
691+ List <MavenBackingRepository > snapshotLocal = MavenBackingRepository .create (snapshotDir .toURI ()
692+ .toString (), reporter , localRepo , client );
693+ snapshot .addAll (snapshotLocal );
694+ sonatypeSnapshotUrl = snapshotUrl ;
695+ }
696+ }
697+ case NONE -> {
698+ if (stagingUrl == null ) {
699+ release = MavenBackingRepository .create (releaseUrl , reporter , localRepo , client );
700+ } else {
701+ release = MavenBackingRepository .create (releaseUrl , reporter , localRepo , client );
702+ staging = MavenBackingRepository .getBackingRepository (stagingUrl , reporter , localRepo , client );
703+ }
704+ if (snapshotUrl != null ) {
705+ snapshot = MavenBackingRepository .create (snapshotUrl , reporter , localRepo , client );
706+ }
707+ }
662708 }
663709
710+
664711 for (MavenBackingRepository mbr : release ) {
665712 if (mbr .isRemote ()) {
666713 remote = true ;
@@ -675,9 +722,14 @@ synchronized boolean init() {
675722 }
676723 }
677724
678- storage = new MavenRepository (localRepo , name , release , staging , snapshot ,
679- client .promiseFactory ()
725+ storage = new MavenRepository (localRepo , name , release , staging , snapshot , client .promiseFactory ()
680726 .executor (), reporter );
727+ MavenRepository storageMvn = (MavenRepository ) storage ;
728+ storageMvn .setSonatypeMode (sonatypeMode );
729+ if (sonatypeReleaseUrl != null ) {
730+ storageMvn .setSonatypePublisherUrl (sonatypeReleaseUrl );
731+ storageMvn .setSonatypePublishSnapshotUrl (sonatypeSnapshotUrl );
732+ }
681733
682734 File indexFile = getIndexFile ();
683735 Processor domain = (registry != null ) ? registry .getPlugin (Processor .class ) : null ;
@@ -1085,4 +1137,8 @@ public boolean isRemote() {
10851137 return remote ;
10861138 }
10871139
1140+ public HttpClient getClient () {
1141+ return client ;
1142+ }
1143+
10881144}
0 commit comments