Skip to content

Commit

Permalink
- install source jar when it is found - fixes #6
Browse files Browse the repository at this point in the history
- set the remote repos on the artifact request so they can be downloaded - fixes #7
  • Loading branch information
vietj committed Jan 19, 2016
1 parent b429683 commit b7db853
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 21 deletions.
11 changes: 11 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ The `out` configuration changes the module output directory:
</configuration>
----

When the dependency has a _sources-jar_, it is imported as well as a sources jar, this can be prevented if necessary:

----
<moduleImport>
...
<sources>false</sources>
...
</moduleImport>
----


=== Compile Ceylon modules

The `compile` goal compiles Ceylon modules:
Expand Down
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@
<artifactId>file-management</artifactId>
<version>1.2</version>
</dependency>
<!--
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
-->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
Expand Down Expand Up @@ -133,7 +135,7 @@
<addTestClassPath>true</addTestClassPath>
<postBuildHookScript>verify</postBuildHookScript>
<debug>true</debug>
<invokerTest>compile-default</invokerTest>
<invokerTest>import-dependency-missing-sources</invokerTest>
</configuration>
</execution>
</executions>
Expand Down
4 changes: 3 additions & 1 deletion src/it/projects/import-dependency-default/verify.groovy
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
def root = new File(basedir, 'target/modules/javax/inject/javax/inject/1')
return new File(root, 'javax.inject.javax.inject-1.jar').exists() &&
new File(root, 'javax.inject.javax.inject-1.jar.sha1').exists()
new File(root, 'javax.inject.javax.inject-1.jar.sha1').exists() &&
new File(root, 'javax.inject.javax.inject-1-sources.jar').exists() &&
new File(root, 'javax.inject.javax.inject-1-sources.jar.sha1').exists()
42 changes: 42 additions & 0 deletions src/it/projects/import-dependency-missing-sources/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.ceylon-lang.test</groupId>
<artifactId>compile-default</artifactId>
<version>1.2.1-SNAPSHOT</version>

<name>compile-default</name>

<build>
<plugins>
<plugin>
<groupId>org.ceylon-lang</groupId>
<artifactId>ceylon-maven-plugin</artifactId>
<version>1.2.1-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>import-dependency</goal>
</goals>
<configuration>
<moduleImports>
<moduleImport>
<!-- 1.0 does not have -sources.jar -->
<dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>1.0</version>
</dependency>
</moduleImport>
</moduleImports>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def root = new File(basedir, 'target/modules/javax/portlet/portlet-api/1.0')
return new File(root, 'javax.portlet.portlet-api-1.0.jar').exists() &&
new File(root, 'javax.portlet.portlet-api-1.0.jar.sha1').exists() &&
!new File(root, 'javax.portlet.portlet-api-1.0-sources.jar').exists() &&
!new File(root, 'javax.portlet.portlet-api-1.0-sources.jar.sha1').exists()
42 changes: 42 additions & 0 deletions src/it/projects/import-dependency-no-sources/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.ceylon-lang.test</groupId>
<artifactId>compile-default</artifactId>
<version>1.2.1-SNAPSHOT</version>

<name>compile-default</name>

<build>
<plugins>
<plugin>
<groupId>org.ceylon-lang</groupId>
<artifactId>ceylon-maven-plugin</artifactId>
<version>1.2.1-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>import-dependency</goal>
</goals>
<configuration>
<moduleImports>
<moduleImport>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<sources>false</sources>
</moduleImport>
</moduleImports>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
5 changes: 5 additions & 0 deletions src/it/projects/import-dependency-no-sources/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def root = new File(basedir, 'target/modules/javax/inject/javax/inject/1')
return new File(root, 'javax.inject.javax.inject-1.jar').exists() &&
new File(root, 'javax.inject.javax.inject-1.jar.sha1').exists() &&
!new File(root, 'javax.inject.javax.inject-1-sources.jar').exists() &&
!new File(root, 'javax.inject.javax.inject-1-sources.jar.sha1').exists()
2 changes: 1 addition & 1 deletion src/it/projects/run-no-module/verify.groovy
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def log = new File(basedir, 'build.log').text
return log.contains('Missing module: mymodule/1.0.0')
return log.contains('Could not find module: mymodule/1.0.0')
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.internal.impl.DefaultRepositorySystem;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;

import java.io.File;
Expand All @@ -51,6 +53,9 @@ public class CeylonImportDependencyMojo extends AbstractMojo {
@Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true)
protected RepositorySystemSession repoSession;

@Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true)
public List<RemoteRepository> remoteRepos;

@Parameter(defaultValue = "${project}", readonly = true)
public MavenProject project;

Expand All @@ -60,6 +65,14 @@ public class CeylonImportDependencyMojo extends AbstractMojo {
@Component
protected ProjectDependenciesResolver resolver;

private ArtifactResult fetch(Dependency dependency) throws Exception {
Artifact artifact = new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), dependency.getType(), dependency.getVersion());
ArtifactRequest request = new ArtifactRequest();
request.setArtifact(artifact);
request.setRepositories(remoteRepos);
return repoSystem.resolveArtifact(repoSession, request);
}

public void execute() throws MojoExecutionException, MojoFailureException {

// Resolve project dependencies
Expand All @@ -80,32 +93,19 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// Prepare all imports
for (ModuleImport moduleImport : moduleImports) {
Dependency dependency = moduleImport.getDependency();
ArtifactResult result;
String dependencyVersion = dependency.getVersion();

// Not provided => resolve from project dependencies
if (dependencyVersion == null) {
if (dependency.getVersion() == null) {
for (org.eclipse.aether.graph.Dependency managed : dependencies) {
if (safeEquals(managed.getArtifact().getGroupId(), dependency.getGroupId()) &&
safeEquals(managed.getArtifact().getArtifactId(), dependency.getArtifactId()) &&
safeEquals(managed.getArtifact().getClassifier(), dependency.getClassifier()) &&
safeEquals(managed.getArtifact().getExtension(), dependency.getType())) {
dependencyVersion = managed.getArtifact().getBaseVersion();
dependency.setVersion(managed.getArtifact().getBaseVersion());
}
}
}

try {
Artifact artifact = new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), dependency.getType(), dependencyVersion);
ArtifactRequest request = new ArtifactRequest();
request.setArtifact(artifact);
request.setRepositories(Collections.<RemoteRepository>emptyList());
result = repoSystem.resolveArtifact(repoSession, request);
} catch (Exception e) {
MojoExecutionException ex = new MojoExecutionException("Cannot resolve dependency");
ex.initCause(e);
throw ex;
}
CeylonImportJarTool tool = new CeylonImportJarTool();
if (moduleImport.getDescriptor() != null) {
tool.setDescriptor(moduleImport.getDescriptor());
Expand All @@ -115,16 +115,35 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
tool.setCwd(cwd);
tool.setOut(out);
tool.setFile(result.getArtifact().getFile());
try {
ArtifactResult result = fetch(dependency);
tool.setFile(result.getArtifact().getFile());
} catch (Exception e) {
MojoExecutionException ex = new MojoExecutionException("Cannot resolve dependency");
ex.initCause(e);
throw ex;
}
if (moduleImport.getSources()) {
try {
dependency.setClassifier("sources");
ArtifactResult result = fetch(dependency);
tool.setSourceJarFile(result.getArtifact().getFile());
} catch (ArtifactResolutionException e) {
// No sources
} catch (Exception e) {
MojoExecutionException ex = new MojoExecutionException("Cannot resolve dependency");
ex.initCause(e);
throw ex;
}
}

//
String moduleName = moduleImport.getName();
String moduleVersion = moduleImport.getVersion();
if (moduleName == null) {
moduleName = dependency.getGroupId() + "." + dependency.getArtifactId();
}
if (moduleVersion == null) {
moduleVersion = dependencyVersion;
moduleVersion = dependency.getVersion();
}
ModuleSpec moduleSpec = new ModuleSpec(moduleName, moduleVersion);
moduleSpecs.add(moduleSpec);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/redhat/ceylon/maven/ModuleImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ModuleImport {
private String name;
private String version;
private File descriptor;
private boolean sources = true;

public Dependency getDependency() {
return dependency;
Expand Down Expand Up @@ -54,4 +55,12 @@ public File getDescriptor() {
public void setDescriptor(File descriptor) {
this.descriptor = descriptor;
}

public boolean getSources() {
return sources;
}

public void setSources(boolean sources) {
this.sources = sources;
}
}

0 comments on commit b7db853

Please sign in to comment.