Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,10 @@ else if ( sourceRoots.contains( filePath ) )

if ( descriptor == null )
{
getLog().warn( "Can't locate " + file );
if ( ! Files.isDirectory( Paths.get ( file ) ) )
{
getLog().warn( "Can't locate " + file );
}
}
else if ( !values[0].equals( descriptor.name() ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -208,20 +210,82 @@ protected void preparePaths( Set<File> sourceFiles )
{
File mainOutputDirectory = new File( getProject().getBuild().getOutputDirectory() );

File mainModuleDescriptor = new File( mainOutputDirectory, "module-info.class" );
File mainModuleDescriptorClassFile = new File( mainOutputDirectory, "module-info.class" );
JavaModuleDescriptor mainModuleDescriptor = null;

File testModuleDescriptorJavaFile = new File( "module-info.java" );
JavaModuleDescriptor testModuleDescriptor = null;

boolean hasTestModuleDescriptor = false;

// Go through the source files to respect includes/excludes
for ( File sourceFile : sourceFiles )
{
// @todo verify if it is the root of a sourcedirectory?
if ( "module-info.java".equals( sourceFile.getName() ) )
{
hasTestModuleDescriptor = true;
testModuleDescriptorJavaFile = sourceFile;
break;
}
}

// Get additional information from the main module descriptor, if available
if ( mainModuleDescriptorClassFile.exists() )
{
ResolvePathsResult<String> result;

try
{
ResolvePathsRequest<String> request =
ResolvePathsRequest.withStrings( testPath )
.setMainModuleDescriptor( mainModuleDescriptorClassFile.getAbsolutePath() );

Toolchain toolchain = getToolchain();
if ( toolchain != null && toolchain instanceof DefaultJavaToolChain )
{
request.setJdkHome( ( (DefaultJavaToolChain) toolchain ).getJavaHome() );
}

result = locationManager.resolvePaths( request );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}

mainModuleDescriptor = result.getMainModuleDescriptor();

pathElements = new LinkedHashMap<String, JavaModuleDescriptor>( result.getPathElements().size() );
pathElements.putAll( result.getPathElements() );

modulepathElements = result.getModulepathElements().keySet();
classpathElements = result.getClasspathElements();
}

// Get additional information from the test module descriptor, if available
if ( testModuleDescriptorJavaFile.exists() )
{
ResolvePathsResult<String> result;

try
{
ResolvePathsRequest<String> request =
ResolvePathsRequest.withStrings( testPath )
.setMainModuleDescriptor( testModuleDescriptorJavaFile.getAbsolutePath() );

Toolchain toolchain = getToolchain();
if ( toolchain != null && toolchain instanceof DefaultJavaToolChain )
{
request.setJdkHome( ( (DefaultJavaToolChain) toolchain ).getJavaHome() );
}

result = locationManager.resolvePaths( request );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}

testModuleDescriptor = result.getMainModuleDescriptor();
}

if ( release != null )
{
Expand All @@ -241,17 +305,55 @@ else if ( Double.valueOf( getTarget() ) < Double.valueOf( MODULE_INFO_TARGET ) )
return;
}

if ( hasTestModuleDescriptor )
if ( testModuleDescriptor != null )
{
modulepathElements = testPath;
classpathElements = Collections.emptyList();

if ( mainModuleDescriptor.exists() )
if ( mainModuleDescriptor != null )
{
// maybe some extra analysis required
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Main and test module descriptors exist:" );
getLog().debug( " main module = " + mainModuleDescriptor.name() );
getLog().debug( " test module = " + testModuleDescriptor.name() );
}

if ( testModuleDescriptor.name().equals( mainModuleDescriptor.name() ) )
{
if ( compilerArgs == null )
{
compilerArgs = new ArrayList<String>();
}
compilerArgs.add( "--patch-module" );

StringBuilder patchModuleValue = new StringBuilder();
patchModuleValue.append( testModuleDescriptor.name() );
patchModuleValue.append( '=' );

for ( String root : getProject().getCompileSourceRoots() )
{
if ( Files.exists( Paths.get( root ) ) )
{
patchModuleValue.append( root ).append( PS );
}
}

compilerArgs.add( patchModuleValue.toString() );
}
else
{
getLog().debug( "Black-box testing - all is ready to compile" );
}

}
else
{
// No main binaries available? Means we're a test-only project.
if ( !mainOutputDirectory.exists() )
{
return;
}
// very odd
// Means that main sources must be compiled with -modulesource and -Xmodule:<moduleName>
// However, this has a huge impact since you can't simply use it as a classpathEntry
Expand All @@ -262,44 +364,16 @@ else if ( Double.valueOf( getTarget() ) < Double.valueOf( MODULE_INFO_TARGET ) )
}
else
{
if ( mainModuleDescriptor.exists() )
if ( mainModuleDescriptor != null )
{
ResolvePathsResult<String> result;

try
{
ResolvePathsRequest<String> request =
ResolvePathsRequest.withStrings( testPath )
.setMainModuleDescriptor( mainModuleDescriptor.getAbsolutePath() );

Toolchain toolchain = getToolchain();
if ( toolchain != null && toolchain instanceof DefaultJavaToolChain )
{
request.setJdkHome( ( (DefaultJavaToolChain) toolchain ).getJavaHome() );
}

result = locationManager.resolvePaths( request );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}

JavaModuleDescriptor moduleDescriptor = result.getMainModuleDescriptor();

pathElements = new LinkedHashMap<String, JavaModuleDescriptor>( result.getPathElements().size() );
pathElements.putAll( result.getPathElements() );

modulepathElements = result.getModulepathElements().keySet();
classpathElements = result.getClasspathElements();


if ( compilerArgs == null )
{
compilerArgs = new ArrayList<String>();
}
compilerArgs.add( "--patch-module" );

StringBuilder patchModuleValue = new StringBuilder( moduleDescriptor.name() )
StringBuilder patchModuleValue = new StringBuilder( mainModuleDescriptor.name() )
.append( '=' )
.append( mainOutputDirectory )
.append( PS );
Expand All @@ -311,7 +385,7 @@ else if ( Double.valueOf( getTarget() ) < Double.valueOf( MODULE_INFO_TARGET ) )
compilerArgs.add( patchModuleValue.toString() );

compilerArgs.add( "--add-reads" );
compilerArgs.add( moduleDescriptor.name() + "=ALL-UNNAMED" );
compilerArgs.add( mainModuleDescriptor.name() + "=ALL-UNNAMED" );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ public void testCompilerBasic()
assertTrue( testClass.exists() );
}

/**
* tests the ability of the plugin to compile a jpms project
*
* @throws Exception
*/
public void testCompilerJpms()
throws Exception
{
CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-jpms-test/plugin-config.xml" );

compileMojo.execute();

assertTrue( new File( compileMojo.getOutputDirectory(), "module-info.class" ).exists() );
assertTrue( new File( compileMojo.getOutputDirectory(), "foo/TestJpms0.class" ).exists() );

TestCompilerMojo testCompileMojo =
getTestCompilerMojo( compileMojo, "target/test-classes/unit/compiler-jpms-test/plugin-config.xml" );

testCompileMojo.execute();

assertTrue( new File( testCompileMojo.getOutputDirectory(), "module-info.class" ).exists() );
assertTrue( new File( testCompileMojo.getOutputDirectory(), "foo/TestJpms0.class" ).exists() );
assertTrue( new File( testCompileMojo.getOutputDirectory(), "foo/TestJpms0Test.class" ).exists() );
}

/**
* tests the ability of the plugin to respond to empty source
*
Expand Down Expand Up @@ -415,6 +440,8 @@ private TestCompilerMojo getTestCompilerMojo( CompilerMojo compilerMojo, String
setVariableValueToObject( mojo, "compileSourceRoots", Collections.singletonList( testSourceRoot ) );

MavenProject project = getMockMavenProject();
project.setFile( testPom );
project.addCompileSourceRoot("/src/main/java" );
project.setArtifacts( Collections.singleton( junitArtifact ) );
project.getBuild().setOutputDirectory( new File( buildDir, "classes" ).getAbsolutePath() );
setVariableValueToObject( mojo, "project", project );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compileSourceRoots>
<compileSourceRoot>${basedir}/target/test-classes/unit/compiler-jpms-test/src/main/java</compileSourceRoot>
</compileSourceRoots>
<compilerId>javac</compilerId>
<debug>true</debug>
<release>9</release>
<outputDirectory>${basedir}/target/test/unit/compiler-jpms-test/target/classes</outputDirectory>
<buildDirectory>${basedir}/target/test/unit/compiler-jpms-test/target</buildDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package foo;

public class TestJpms0
{

public TestJpms0()
{

System.out.println( "Woo Hoo!" );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

module foo {
// empty
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package foo;

import junit.framework.TestCase;

public class TestJpms0Test
extends TestCase
{
public void testJpms0Test()
{
TestJpms0 test = new TestJpms0();
}
}
Loading