यदि आप एक कस्टम प्लगइन का उपयोग करने के इच्छुक हैं, तो आप मानक तैनाती के रूप में एक ही समय में "दर्पण" स्थानों की सूची में तैनात करने के लिए मेवेन को कॉन्फ़िगर कर सकते हैं। मैं इसे प्रोफाइल में परिभाषित करने की अनुशंसा करता हूं ताकि आप नियंत्रित कर सकें कि कौन से परिनियोजन प्रतिबिंबित किए गए हैं (यह हर निर्माण पर ऐसा करने के लिए उचित नहीं हो सकता है)।
आप एक नया Maven परियोजना बनाने और पोम निर्दिष्ट करने की आवश्यकता के लिए एक नया प्लगइन को परिभाषित करने के Maven-प्लगइन पैकेजिंग है:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>name.seller.rich</groupId>
<artifactId>maven-mirror-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
src/मुख्य/जावा में एक मोजो परिभाषित करते हैं। नीचे दिया गया कोड "दर्पण" लक्ष्य घोषित करता है, यह आर्टिफैक्ट परिनियोजन को दर्पण करने के लिए दर्पण रिपोजिटरी आइटम (एक रिपोजिटरीआईडी और यूआरएल युक्त) की एक सूची लेता है। प्लगइन मेवेन-तैनाती-प्लगइन के रूप में तैनाती के लिए एक ही दृष्टिकोण का उपयोग करता है, और अधिकांश पैरामीटर लेता है।
ध्यान दें कि तैनाती करने के लिए उचित अनुमतियों के साथ आपको प्रत्येक रिपॉजिटरी के लिए अपनी सेटिंग्स.एक्सएमएल में एक सर्वर को परिभाषित करने की आवश्यकता है या निर्माण विफल हो जाएगा!
package name.seller.rich;
public class MirrorRepository {
private String repositoryId;
private String url;
public String getRepositoryId() {
return repositoryId;
}
public void setRepositoryId(String repositoryId) {
this.repositoryId = repositoryId;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
यहाँ प्लगइन का उपयोग कर एक उदाहरण विन्यास है:
package name.seller.rich;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.deployer.ArtifactDeployer;
import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
/**
* @goal mirror
* @phase deploy
*/
public class MirrorMojo extends AbstractMojo {
/**
* @parameter expression=
* "${component.org.apache.maven.artifact.deployer.ArtifactDeployer}"
* @required
* @readonly
*/
private ArtifactDeployer deployer;
/**
* Map that contains the layouts
*
* @component role=
* "org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
*/
private Map repositoryLayouts;
/**
* Component used to create a repository
*
* @component
*/
private ArtifactRepositoryFactory repositoryFactory;
/**
* The type of remote repository layout to deploy to. Try <i>legacy</i> for
* a Maven 1.x-style repository layout.
*
* @parameter expression="${repositoryLayout}" default-value="default"
* @required
*/
private String repositoryLayout;
/**
* Parameter used to update the metadata to make the artifact as release.
*
* @parameter expression="${updateReleaseInfo}" default-value="false"
*/
private boolean updateReleaseInfo;
/**
* Whether to deploy snapshots with a unique version or not.
*
* @parameter expression="${uniqueVersion}" default-value="true"
*/
private boolean uniqueVersion;
/**
* @parameter expression="${mirrorRepositories}"
* @required
*/
private MirrorRepository[] mirrorRepositories;
/**
* @parameter expression="${localRepository}"
* @required
* @readonly
*/
private ArtifactRepository localRepository;
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* Deploy all artifacts for the project to each mirror repository.
*/
public void execute() throws MojoExecutionException, MojoFailureException {
ArtifactRepositoryLayout layout;
layout = (ArtifactRepositoryLayout) repositoryLayouts
.get(repositoryLayout);
for (int i = 0; i < mirrorRepositories.length; i++) {
MirrorRepository mirrorRepository = mirrorRepositories[i];
ArtifactRepository deploymentRepository = repositoryFactory
.createDeploymentArtifactRepository(mirrorRepository
.getRepositoryId(), mirrorRepository.getUrl(),
layout, uniqueVersion);
String protocol = deploymentRepository.getProtocol();
if ("".equals(protocol) || protocol == null) {
throw new MojoExecutionException("No transfer protocol found.");
}
deployToRepository(deploymentRepository);
}
}
/**
* Deploy all artifacts to the passed repository.
*/
private void deployToRepository(ArtifactRepository repo)
throws MojoExecutionException {
String protocol = repo.getProtocol();
if (protocol.equalsIgnoreCase("scp")) {
File sshFile = new File(System.getProperty("user.home"), ".ssh");
if (!sshFile.exists()) {
sshFile.mkdirs();
}
}
File pomFile = project.getFile();
Artifact artifact = project.getArtifact();
// Deploy the POM
boolean isPomArtifact = "pom".equals(project.getPackaging());
if (!isPomArtifact) {
ArtifactMetadata metadata = new ProjectArtifactMetadata(artifact,
pomFile);
artifact.addMetadata(metadata);
}
if (updateReleaseInfo) {
artifact.setRelease(true);
}
try {
List attachedArtifacts = project.getAttachedArtifacts();
if (isPomArtifact) {
deployer.deploy(pomFile, artifact, repo, localRepository);
} else {
File file = artifact.getFile();
if (file != null && !file.isDirectory()) {
deployer.deploy(file, artifact, repo, localRepository);
} else if (!attachedArtifacts.isEmpty()) {
getLog()
.info(
"No primary artifact to deploy, deploy attached artifacts instead.");
} else {
String message = "The packaging for this project did not assign a file to the build artifact";
throw new MojoExecutionException(message);
}
}
for (Iterator i = attachedArtifacts.iterator(); i.hasNext();) {
Artifact attached = (Artifact) i.next();
deployer.deploy(attached.getFile(), attached, repo,
localRepository);
}
} catch (ArtifactDeploymentException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}
मोजो का संदर्भ repositoryId और यूआरएल को संपुटित करने के लिए एक MirrorRepository प्रकार, यह एक साधारण सेम है। नोट सभी तैनाती प्रारूप समर्थित हैं (http, scp, ftp):
<plugin>
<groupId>name.seller.rich</groupId>
<artifactId>maven-mirror-plugin</artifactId>
<executions>
<execution>
<id>mirror</id>
<phase>deploy</phase>
<goals>
<goal>mirror</goal>
</goals>
</execution>
</executions>
<configuration>
<mirrorRepositories>
<mirrorRepository>
<repositoryId>mirror</repositoryId>
<url>http://path/to/mirror</url>
</mirrorRepository>
</mirrorRepositories>
<!--any other deploy configuration needed-->
</configuration>
</plugin>
धन्यवाद! साथ अगर आप पहले से ही अन्य बातों के लिए प्रोफ़ाइल का उपयोग कर रहे हैं, तो आप शायद संपत्ति आधारित सक्रियण चाहता हूँ (जैसे ' repo2 ' repo2 सक्रिय करने के लिए, और इसी प्रकार - यह मेरे लिए अच्छी तरह से काम किया, एक परिवर्तन के साथ! repo2 के लिए, फिर केवल '-Drepo2') –
या' वितरण प्रबंधन 'में सीधे' वितरण प्रबंधन 'डालें, यदि आप गुणों का उपयोग नहीं करना चाहते हैं –