現在、Nexusに多くのレガシーjarをデプロイするJavaプログラムを作成しています。私のアプローチは、これは非常に遅いですNexusに成果物をプログラム的にデプロイするための素早い方法(Java版)
mvn deploy:deploy-file ...
コマンドラインでdeploy:deploy-file
目標を開始し、プロセスを起動することです。これを行うより速い方法があるのだろうか?
現在、Nexusに多くのレガシーjarをデプロイするJavaプログラムを作成しています。私のアプローチは、これは非常に遅いですNexusに成果物をプログラム的にデプロイするための素早い方法(Java版)
mvn deploy:deploy-file ...
コマンドラインでdeploy:deploy-file
目標を開始し、プロセスを起動することです。これを行うより速い方法があるのだろうか?
あなたは、具体的ネクサスをターゲットにする場合、あなたがアップロードを実行するためにtheir REST APIを使用する方が簡単かもしれません:
ここではカールを使用して、いくつかの例です。
アーティファクトをアップロードし、POMファイル生成
:あなたは、Javaプログラムで
curl -v -F r=releases -F hasPom=true -F e=jar -F [email protected] -F [email protected] -u admin:admin123 http://localhost:8081/nexus/service/local/artifact/maven/content
:POMファイルとアーティファクトをアップロードする
curl -v -F r=releases -F hasPom=false -F e=jar -F g=com.test -F a=project -F v=1.0 -F p=jar -F [email protected] -u admin:admin123 http://localhost:8081/nexus/service/local/artifact/maven/content
をHttpURLConnection
を使用してPOST呼び出しを行います(example of that hereとauthentication hereとdocumentation of cURL here)。基本的には、POSTパラメータに、あなたは(あなたがそれでPOMをアップロードしている場合やfalse
)r=releases
、hasPom=true
を持っている必要があり、アーティファクトの延長e
、g
、a
、v
とp
座標の(のgroupId、たartifactId 、バージョンとパッケージ)、最後にfile
を展開します。
スナップショットbecause it is explicitely disabledをアップロードすることはできません。
あなたはどんなアーティファクトのために、そして何でもリモートリポジトリのために働くだろう、より汎用的なソリューションを、(たとえローカル1)したい場合は、直接のMavenでのシーンの下で使用されているオードのAPIを使用することができます3.1以上。チームは、DeployArtifacts
サンプルにそのようなタスクの例を示します。
プロジェクトにオードの依存関係を追加します。
<dependencies>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-file</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-http</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>${mavenVersion}</version>
</dependency>
</dependencies>
<properties>
<aetherVersion>1.1.0</aetherVersion>
<mavenVersion>3.3.9</mavenVersion>
</properties>
そしてあなたがアーティファクトをデプロイするには、次のコードを持つことができます。 public static void main(String[] args) throws DeploymentException {
RepositorySystem system = newRepositorySystem();
RepositorySystemSession session = newSession(system);
Artifact artifact = new DefaultArtifact("groupId", "artifactId", "classifier", "extension", "version");
artifact = artifact.setFile(new File("/path/to/file"));
// add authentication to connect to remove repository
Authentication authentication = new AuthenticationBuilder().addUsername("username").addPassword("password").build();
// creates a remote repo at the given URL to deploy to
RemoteRepository distRepo = new RemoteRepository.Builder("id", "default", "url").setAuthentication(authentication).build();
DeployRequest deployRequest = new DeployRequest();
deployRequest.addArtifact(artifact);
deployRequest.setRepository(distRepo);
system.deploy(session, deployRequest);
}
private static RepositorySystem newRepositorySystem() {
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, FileTransporterFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
return locator.getService(RepositorySystem.class);
}
private static RepositorySystemSession newSession(RepositorySystem system) {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepo = new LocalRepository("target/local-repo");
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
return session;
}
このコードは、与えられた座標を有する単一のアーティファクトを展開します( groupIdを構成リモートリポジトリに、たartifactId、種類、分類器及びバージョン):
""
をクラシファイアとして使用できます。Artifact
の方法setFile
で設定します。"default"
レイアウトは、Maven 2リポジトリで使用されるレイアウトです(Maven 1では"legacy"
レイアウトとは異なります)。 URLはdeploy-file
goalの内部で使用するURLと同じなので、file:///C:/m2-repo
またはscp://host.com/path/to/repo
です。Authentication
を作成して、リモートリポジトリに接続することができます(スニペットに示されています)。あなたがそれを添付アーティファクトを展開したい場合は、POMファイルと同様に、あなたがSubArtifact
を作成することができます。これは、上記のように構成されたアーティファクトに、分類器なしで、POMアーティファクトを添付します
Artifact pomArtifact = new SubArtifact(artifact, "", "pom");
pomArtifact = pomArtifact.setFile(new File("pom.xml"));
。次に、主なものと同様に展開要求に追加することができます。
deployRequest.addArtifact(artifact).addArtifact(pomArtifact);
これらの両方が展開されます。
Eclipse Aether APIを使用して、プログラムでJavaでプログラムを実行できます。詳細については、私のMaven Repository Toolsの出典を確認してください。事実、すべての成果物がすでにMavenリポジトリ形式のローカルフォルダにある場合は、必要に応じてすぐにそのまま使用することができます。
具体的に展開関連コードが
であります