Skip to content

Commit ec97f4d

Browse files
rehevkor5slawekjaranowski
authored andcommitted
Add "provided" classpathScope (runtime+provided)
- New "provided" value for classpathScope includes both runtime and provided dependencies - Closes #60
1 parent d18ed80 commit ec97f4d

File tree

11 files changed

+293
-2
lines changed

11 files changed

+293
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.codehaus.mojo.exec.it</groupId>
6+
<artifactId>parent</artifactId>
7+
<version>0.1</version>
8+
</parent>
9+
10+
<groupId>org.codehaus.mojo.exec-maven-plugin</groupId>
11+
<artifactId>git-issue-60-exec</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.slf4j</groupId>
17+
<artifactId>slf4j-api</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.slf4j</groupId>
21+
<artifactId>slf4j-log4j12</artifactId>
22+
<scope>runtime</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>log4j</groupId>
26+
<artifactId>log4j</artifactId>
27+
<scope>provided</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<!-- for manual tests. Can't be automated in the unit tests as the plugin's not installed. What about integration tests? -->
33+
<plugins>
34+
<plugin>
35+
<groupId>org.codehaus.mojo</groupId>
36+
<artifactId>exec-maven-plugin</artifactId>
37+
<version>@project.version@</version>
38+
<executions>
39+
<execution>
40+
<phase>test</phase>
41+
<goals>
42+
<goal>exec</goal>
43+
</goals>
44+
</execution>
45+
</executions>
46+
<configuration>
47+
<classpathScope>provided</classpathScope>
48+
<executable>${JAVA_HOME}/bin/java</executable>
49+
<arguments>
50+
<argument>-classpath</argument>
51+
<classpath />
52+
<argument>gitissue60.Main</argument>
53+
</arguments>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package gitissue60;
2+
3+
/*
4+
* Copyright 2005 The Codehaus.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
/**
23+
* @version $Id$
24+
*/
25+
public class Main
26+
{
27+
private static final Logger LOGGER = LoggerFactory.getLogger( Main.class );
28+
public static void main( String[] args ) throws Exception {
29+
LOGGER.info( "Can you hear me, Major Tom?" );
30+
}
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Root logger option
2+
log4j.rootLogger=INFO, file
3+
4+
# Direct log messages to stdout
5+
log4j.appender.file=org.apache.log4j.FileAppender
6+
log4j.appender.file.File=exec.log
7+
log4j.appender.file.layout=org.apache.log4j.PatternLayout
8+
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
File execLog = new File(basedir, 'exec.log')
21+
assert execLog.exists()
22+
assert execLog.getText().contains('Can you hear me, Major Tom?')
23+
24+
File buildLog = new File(basedir, 'build.log')
25+
assert buildLog.exists()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.debug = true
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>org.codehaus.mojo.exec.it</groupId>
5+
<artifactId>parent</artifactId>
6+
<version>0.1</version>
7+
</parent>
8+
9+
<groupId>org.codehaus.mojo.exec-maven-plugin</groupId>
10+
<artifactId>git-issue-60-java</artifactId>
11+
<version>0.0.1-SNAPSHOT</version>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.slf4j</groupId>
16+
<artifactId>slf4j-api</artifactId>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.slf4j</groupId>
20+
<artifactId>slf4j-log4j12</artifactId>
21+
<scope>runtime</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>log4j</groupId>
25+
<artifactId>log4j</artifactId>
26+
<scope>provided</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<!-- for manual tests. Can't be automated in the unit tests as the plugin's not installed. What about integration tests? -->
37+
<plugins>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>@project.version@</version>
42+
<executions>
43+
<execution>
44+
<phase>test</phase>
45+
<goals>
46+
<goal>java</goal>
47+
</goals>
48+
</execution>
49+
</executions>
50+
<configuration>
51+
<mainClass>gitissue60.Main</mainClass>
52+
<classpathScope>provided</classpathScope>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package gitissue60;
2+
3+
/*
4+
* Copyright 2005 The Codehaus.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
/**
23+
* @version $Id$
24+
*/
25+
public class Main
26+
{
27+
private static final Logger LOGGER = LoggerFactory.getLogger( Main.class );
28+
public static void main( String[] args ) throws Exception {
29+
LOGGER.info( "Can you hear me, Major Tom?" );
30+
}
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Root logger option
2+
log4j.rootLogger=INFO, file
3+
4+
# Direct log messages to stdout
5+
log4j.appender.file=org.apache.log4j.FileAppender
6+
log4j.appender.file.File=exec.log
7+
log4j.appender.file.layout=org.apache.log4j.PatternLayout
8+
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
File execLog = new File(basedir, 'exec.log')
21+
assert execLog.exists()
22+
assert execLog.getText().contains('Can you hear me, Major Tom?')
23+
24+
File buildLog = new File(basedir, 'build.log')
25+
assert buildLog.exists()
26+
def buildLogText = buildLog.getText()
27+
assert buildLogText.contains( "Adding project dependency artifact: slf4j-api to classpath" )
28+
assert buildLogText.contains( "Adding project dependency artifact: log4j to classpath" )
29+
assert buildLogText.contains( "Adding project dependency artifact: slf4j-log4j12 to classpath" )
30+
assert !buildLogText.contains( "Adding project dependency artifact: junit to classpath" )

src/it/projects/setup-parent/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@
3030
<artifactId>commons-io</artifactId>
3131
<version>2.7</version>
3232
</dependency>
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-api</artifactId>
36+
<version>1.7.21</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.slf4j</groupId>
40+
<artifactId>slf4j-log4j12</artifactId>
41+
<version>1.7.21</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>log4j</groupId>
45+
<artifactId>log4j</artifactId>
46+
<version>1.2.17</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>3.8.1</version>
52+
</dependency>
3353
</dependencies>
3454
</dependencyManagement>
3555

src/main/java/org/codehaus/mojo/exec/AbstractExecMojo.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import java.io.File;
2323
import java.nio.file.Path;
2424
import java.nio.file.Paths;
25+
import java.util.HashSet;
2526
import java.util.List;
27+
import java.util.Set;
2628

2729
import org.apache.maven.artifact.Artifact;
2830
import org.apache.maven.execution.MavenSession;
@@ -89,8 +91,17 @@ public abstract class AbstractExecMojo extends AbstractMojo {
8991
private String commandlineArgs;
9092

9193
/**
92-
* Defines the scope of the classpath passed to the plugin. Set to compile,test,runtime or system depending on your
93-
* needs. Since 1.1.2, the default value is 'runtime' instead of 'compile'.
94+
* Defines the scope of the classpath passed to the plugin.
95+
*
96+
* <ul>
97+
* <li><code>runtime</code> (default): Include "compile" and "runtime" scopes</li>
98+
* <li><code>compile</code>: Include "compile", "provided", and "system" scopes</li>
99+
* <li><code>test</code>: Include all scopes</li>
100+
* <li><code>provided</code>: Include "compile", "runtime", "provided", and "system" scopes</li>
101+
* <li><code>system</code>: Include "system" scope</li>
102+
* </ul>
103+
*
104+
* Since 1.1.2, the default value is 'runtime' instead of 'compile'.
94105
*/
95106
@Parameter(property = "exec.classpathScope", defaultValue = "runtime")
96107
protected String classpathScope;
@@ -153,6 +164,15 @@ protected void collectProjectArtifactsAndClasspath(List<Artifact> artifacts, Lis
153164
if (addOutputToClasspath) {
154165
theClasspathFiles.add(Paths.get(project.getBuild().getOutputDirectory()));
155166
}
167+
} else if ("provided".equals(classpathScope)) {
168+
// "compile" gives compile, provided, and system scopes
169+
// "runtime" gives compile and runtime scopes
170+
Set<Artifact> artifactSet = new HashSet<>(project.getCompileArtifacts());
171+
artifactSet.addAll(project.getRuntimeArtifacts());
172+
artifacts.addAll(artifactSet);
173+
if (addOutputToClasspath) {
174+
theClasspathFiles.add(Paths.get(project.getBuild().getOutputDirectory()));
175+
}
156176
} else if ("system".equals(classpathScope)) {
157177
artifacts.addAll(project.getSystemArtifacts());
158178
} else {

0 commit comments

Comments
 (0)