Skip to content

Commit a462cd3

Browse files
committed
[MJAVADOC-611] Support adjustment javadoc link of dependency
1 parent 31dad7b commit a462cd3

File tree

6 files changed

+278
-14
lines changed

6 files changed

+278
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
invoker.goals = compile javadoc:javadoc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>foo</groupId>
26+
<artifactId>bar</artifactId>
27+
<version>0.1.0-SNAPSHOT</version>
28+
29+
<properties>
30+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
</properties>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-javadoc-plugin</artifactId>
38+
<version>@project.version@</version>
39+
<configuration>
40+
<detectLinks>true</detectLinks>
41+
<dependencyLinks>
42+
<link>
43+
<groupId>org.ow2.asm</groupId>
44+
<artifactId>asm</artifactId>
45+
<url>https://asm.ow2.io/javadoc</url>
46+
</link>
47+
</dependencyLinks>
48+
</configuration>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
<dependencies>
54+
<dependency>
55+
<groupId>org.ow2.asm</groupId>
56+
<artifactId>asm</artifactId>
57+
<version>7.1</version>
58+
</dependency>
59+
</dependencies>
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package foo;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.objectweb.asm.Opcodes;
23+
24+
/**
25+
* Bar.
26+
*/
27+
public class Bar implements Opcodes
28+
{
29+
public void run()
30+
{
31+
System.out.println( "Bar" );
32+
}
33+
34+
}
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+
def options = new File( basedir, 'target/site/apidocs/options' );
21+
assert options.exists() : options + " not found"
22+
23+
def linkLines = options.readLines().dropWhile{it != '-link'}.drop(1).takeWhile{!it.startsWith('-')}
24+
assert linkLines.size() == 1
25+
assert linkLines[0] == "'https://asm.ow2.io/javadoc'"

src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java

+66-14
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ public abstract class AbstractJavadocMojo
527527
* The added Javadoc <code>-link</code> parameter will be <code>http://commons.apache.org/lang/apidocs</code>.
528528
*
529529
* @see #links
530+
* @see #dependencyLinks
530531
* @since 2.6
531532
*/
532533
@Parameter( property = "detectLinks", defaultValue = "false" )
@@ -1142,9 +1143,31 @@ public abstract class AbstractJavadocMojo
11421143
*
11431144
* @see #detectLinks
11441145
* @see #detectJavaApiLink
1146+
* @see #dependencyLinks
11451147
*/
11461148
@Parameter( property = "links" )
11471149
protected ArrayList<String> links;
1150+
1151+
1152+
/**
1153+
* Redefine the apidoc URL for specific dependencies when using {@link #detectLinks}.
1154+
* Useful if the dependency wasn't build with Maven or when the apidocs have been moved.
1155+
* <pre>
1156+
* &lt;dependencyLinks&gt;
1157+
* &lt;dependencyLink&gt;
1158+
* &lt;groupId&gt;groupId&lt;/groupId&gt;
1159+
* &lt;artifactId&gt;artifactId&lt;/artifactId&gt;
1160+
* &lt;classifier&gt;classifier&lt;/classifier&gt; &lt;!-- optional --&gt;
1161+
* &lt;url&gt;version&lt;/url&gt;
1162+
* &lt;/dependencyLink&gt;
1163+
* &lt;/dependencyLinks&gt;
1164+
* </pre>
1165+
*
1166+
* @see #detectLinks
1167+
* @since 3.3.0
1168+
*/
1169+
@Parameter
1170+
private List<DependencyLink> dependencyLinks = new ArrayList<>();
11481171

11491172
/**
11501173
* Creates an HTML version of each source file (with line numbers) and adds links to them from the standard
@@ -6390,32 +6413,61 @@ private List<String> getDependenciesLinks()
63906413
continue;
63916414
}
63926415

6393-
try
6416+
Optional<DependencyLink> depLink =
6417+
this.dependencyLinks.stream().filter( d -> matches( d, artifact ) ).findAny();
6418+
6419+
final String url;
6420+
final boolean detected;
6421+
if ( depLink.isPresent() )
63946422
{
6395-
MavenProject artifactProject =
6396-
mavenProjectBuilder.build( artifact, getProjectBuildingRequest( project ) ).getProject();
6397-
6398-
if ( StringUtils.isNotEmpty( artifactProject.getUrl() ) )
6423+
url = depLink.get().getUrl();
6424+
detected = false;
6425+
}
6426+
else
6427+
{
6428+
try
63996429
{
6400-
String url = getJavadocLink( artifactProject );
6401-
6402-
if ( isValidJavadocLink( url, true ) )
6403-
{
6404-
getLog().debug( "Added Javadoc link: " + url + " for " + artifactProject.getId() );
6430+
MavenProject artifactProject =
6431+
mavenProjectBuilder.build( artifact, getProjectBuildingRequest( project ) ).getProject();
64056432

6406-
dependenciesLinks.add( url );
6407-
}
6433+
url = getJavadocLink( artifactProject );
6434+
detected = true;
6435+
}
6436+
catch ( ProjectBuildingException e )
6437+
{
6438+
logError( "ProjectBuildingException for " + artifact.toString() + ": " + e.getMessage(), e );
6439+
continue;
64086440
}
64096441
}
6410-
catch ( ProjectBuildingException e )
6442+
6443+
if ( isValidJavadocLink( url, detected ) )
64116444
{
6412-
logError( "ProjectBuildingException for " + artifact.toString() + ": " + e.getMessage(), e );
6445+
getLog().debug( "Added Javadoc link: " + url + " for " + artifact.getId() );
6446+
6447+
dependenciesLinks.add( url );
64136448
}
64146449
}
64156450

64166451
return dependenciesLinks;
64176452
}
64186453

6454+
private boolean matches( DependencyLink d, Artifact artifact )
6455+
{
6456+
if ( d.getGroupId() != null && !d.getGroupId().equals( artifact.getGroupId() ) )
6457+
{
6458+
return false;
6459+
}
6460+
if ( d.getArtifactId() != null && !d.getArtifactId().equals( artifact.getArtifactId() ) )
6461+
{
6462+
return false;
6463+
}
6464+
if ( d.getClassifier() != null && !d.getClassifier().equals( artifact.getClassifier() ) )
6465+
{
6466+
return false;
6467+
}
6468+
return true;
6469+
}
6470+
64196471
/**
64206472
* @return if {@link #detectJavaApiLink}, the Java API link based on the {@link #javaApiLinks} properties and the
64216473
* value of the <code>source</code> parameter in the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.apache.maven.plugins.javadoc;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
/**
23+
* In case of autodetectLinks, this class can overwrite the url for that dependency
24+
*
25+
* @since 3.3.0
26+
*/
27+
public class DependencyLink
28+
{
29+
private String groupId;
30+
31+
private String artifactId;
32+
33+
private String classifier;
34+
35+
private String url;
36+
37+
public String getUrl()
38+
{
39+
return url;
40+
}
41+
42+
public void setUrl( String url )
43+
{
44+
this.url = url;
45+
}
46+
47+
public String getGroupId()
48+
{
49+
return groupId;
50+
}
51+
52+
public void setGroupId( String groupId )
53+
{
54+
this.groupId = groupId;
55+
}
56+
57+
public String getArtifactId()
58+
{
59+
return artifactId;
60+
}
61+
62+
public void setArtifactId( String artifactId )
63+
{
64+
this.artifactId = artifactId;
65+
}
66+
67+
public String getClassifier()
68+
{
69+
return classifier;
70+
}
71+
72+
public void setClassifier( String classifier )
73+
{
74+
this.classifier = classifier;
75+
}
76+
}

0 commit comments

Comments
 (0)