Skip to content

Commit

Permalink
Add support for a local packed osgi-repository
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Jul 18, 2023
1 parent 18c9743 commit 25e7426
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 11 deletions.
4 changes: 4 additions & 0 deletions demo/osgi-repository/repository/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<version>${tycho-version}</version>
<!-- Extensions must be enabled for this mojo to work reliable and for using the custom package type-->
<extensions>true</extensions>
<configuration>
<!-- Another option would be using 'local' -->
<repositoryLayout>maven</repositoryLayout>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
8 changes: 8 additions & 0 deletions tycho-repository-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
<artifactId>org.osgi.util.promise</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
import java.io.IOException;
import java.net.URI;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.zip.ZipArchiver;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.MavenArtifactNamespace;

Expand All @@ -37,8 +42,8 @@
* Generates an OSGi repository from the current reactor projects
*
*/
@Mojo(name = "package-maven-repository")
public class PackageMavenRepositoryMojo extends AbstractMojo {
@Mojo(name = "package-repository")
public class PackageRepositoryMojo extends AbstractMojo {

@Parameter(property = "session", readonly = true)
protected MavenSession session;
Expand All @@ -64,16 +69,47 @@ public class PackageMavenRepositoryMojo extends AbstractMojo {
@Parameter(property = "project", readonly = true)
private MavenProject project;

/**
* Specify the used layout, possible values are:
* <ul>
* <li><code>maven</code> - all artifacts are referenced with the mvn protocol
* and the result can be deployment to a maven repository (either local or
* remote)</li>
* <li><code>local</code> - all artifacts are copied into a folder and
* referenced relative to this folder, the result can be</li>
* </ul>
*/
@Parameter(defaultValue = "maven")
private RepositoryLayout repositoryLayout;

@Component(role = Archiver.class, hint = "zip")
private ZipArchiver zipArchiver;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
XMLResourceGenerator resourceGenerator = new XMLResourceGenerator();
resourceGenerator.name(repositoryName);
File folder;
if (repositoryLayout == RepositoryLayout.local) {
folder = new File(destination, FilenameUtils.getBaseName(repositoryFileName));
folder.mkdirs();
resourceGenerator.base(folder.toURI());
} else {
folder = null;
}
for (MavenProject project : session.getProjects()) {
if (isInteresting(project)) {
ResourceBuilder rb = new ResourceBuilder();
try {
if (rb.addFile(project.getArtifact().getFile(), new URI("mvn:" + project.getGroupId() + ":"
+ project.getArtifactId() + ":" + project.getVersion()))) {
URI uri;
File file = project.getArtifact().getFile();
if (folder == null) {
uri = new URI("mvn:" + project.getGroupId() + ":" + project.getArtifactId() + ":"
+ project.getVersion());
} else {
uri = new File(folder, file.getName()).toURI();
}
if (rb.addFile(project.getArtifact().getFile(), uri)) {
CapReqBuilder identity = new CapReqBuilder(MavenArtifactNamespace.MAVEN_ARTIFACT_NAMESPACE)
.addAttribute(MavenArtifactNamespace.CAPABILITY_GROUP_ATTRIBUTE, project.getGroupId())
.addAttribute(MavenArtifactNamespace.MAVEN_ARTIFACT_NAMESPACE, project.getArtifactId())
Expand All @@ -82,6 +118,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
rb.addCapability(identity);
resourceGenerator.resource(rb.build());
getLog().info("Adding " + project.getId());
if (folder != null) {
FileUtils.copyFileToDirectory(file, folder);
}
} else {
getLog().info("Skip " + project.getId() + ": Not a bundle");
}
Expand All @@ -92,11 +131,22 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}
try {
File location = new File(destination, repositoryFileName);
resourceGenerator.save(location);
Artifact artifact = project.getArtifact();
artifact.setArtifactHandler(new DefaultArtifactHandler("xml"));
artifact.setFile(location);
if (folder != null) {
File location = new File(folder, repositoryFileName);
resourceGenerator.save(location);
File destFile = new File(destination, project.getArtifactId() + "-" + folder.getName() + ".zip");
zipArchiver.addDirectory(folder);
zipArchiver.setDestFile(destFile);
zipArchiver.createArchive();
artifact.setFile(destFile);
artifact.setArtifactHandler(new DefaultArtifactHandler("zip"));
} else {
File location = new File(destination, repositoryFileName);
resourceGenerator.save(location);
artifact.setArtifactHandler(new DefaultArtifactHandler("xml"));
artifact.setFile(location);
}
} catch (IOException e) {
throw new MojoExecutionException("Could not write OSGi Repository!", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.repository.plugin;

public enum RepositoryLayout {
/**
* reference artifacts using mvn: protocol
*/
maven,
/**
* references artifacts as local files and copy them into a folder
*/
local;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.codehaus.plexus.component.annotations.Component;

@Component(role = AbstractMavenLifecycleParticipant.class)
public class PackageMavenLifecycleParticipant extends AbstractMavenLifecycleParticipant {
public class TychoRepositoryPluginMavenLifecycleParticipant extends AbstractMavenLifecycleParticipant {

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
Expand All @@ -40,7 +40,7 @@ private void addInterestingProjects(MavenProject project, List<MavenProject> pro
if (other == project) {
continue;
}
if (PackageMavenRepositoryMojo.isInteresting(other)) {
if (PackageRepositoryMojo.isInteresting(other)) {
Dependency dependency = new Dependency();
dependency.setGroupId(other.getGroupId());
dependency.setArtifactId(other.getArtifactId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<prepare-package>
</prepare-package>
<package>
org.eclipse.tycho:tycho-repository-plugin:${project.version}:package-maven-repository
org.eclipse.tycho:tycho-repository-plugin:${project.version}:package-repository
</package>
<integration-test>
</integration-test>
Expand Down

0 comments on commit 25e7426

Please sign in to comment.