0

环境:Ubuntu 64bit,maven 3.0.4。

编辑:我找到了一种方法,请参阅下面的答案;但与我想出的相比,猴子补丁是优秀设计的缩影......

我使用这个插件来查找与 JDK 一起使用的接口的实现ServiceLoader

我根本不是 maven 专家,我遇到了一个我不知道如何解决的问题......当插件运行时(见下面的输出),它生成的文件没有显示在生成的 jar 中:( 但是它的作用如何target/classes

我已经尝试并在谷歌上搜索了很多次,多次打破了我的 pom,我现在完全不知道如何解决这个问题。我应该如何修改下面我的 pom.xml 的构建部分以使其工作?

<!-- In case it matters: <packaging>bundle</packaging> -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <links>
                    <link>http://docs.oracle.com/javase/6/docs/api</link>
                    <link>http://docs.guava-libraries.googlecode.com/git/javadoc</link>
                    <link>http://fge.github.io/msg-simple</link>
                    <link>http://jsr-305.googlecode.com/svn/trunk/javadoc</link>
                    <link>http://fge.github.com/btf</link>
                </links>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <id>sign-artifacts</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>sign</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <version>2.3.7</version>
        </plugin>
        <plugin>
            <groupId>eu.somatik.serviceloader-maven-plugin</groupId>
            <artifactId>serviceloader-maven-plugin</artifactId>
            <version>1.0.2</version>
            <configuration>
                <services>
                    <param>com.github.fge.msgsimple.serviceloader.MessageBundleProvider</param>
                </services>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

输出(去掉不相关的部分):

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building uri-template 0.5-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:1.0:enforce (enforce-maven) @ uri-template ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ uri-template ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ uri-template ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 31 source files to /home/fge/src/perso/uri-template/target/classes
[INFO] 
[INFO] --- serviceloader-maven-plugin:1.0.2:generate (default) @ uri-template ---
[INFO] Scanning generated classes for implementations...
[INFO] Generating service file /home/fge/src/perso/uri-template/target/classes/META-INF/services/com.github.fge.msgsimple.serviceloader.MessageBundleProvider
[INFO]   + com.github.fge.uritemplate.URITemplateMessageBundle
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ uri-template ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 9 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ uri-template ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 20 source files to /home/fge/src/perso/uri-template/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ uri-template ---
[INFO] Surefire report directory: /home/fge/src/perso/uri-template/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@31c480e8
Tests run: 513, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.402 sec

Results :

Tests run: 513, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --- maven-bundle-plugin:2.3.7:bundle (default-bundle) @ uri-template ---
[INFO] 
[INFO] --- maven-javadoc-plugin:2.9:jar (default) @ uri-template ---
[INFO]
# Stripped javadoc build messages
[INFO] Building jar: /home/fge/src/perso/uri-template/target/uri-template-0.5-SNAPSHOT-javadoc.jar
[INFO]
[INFO] >>> maven-source-plugin:2.2.1:jar (default) @ uri-template >>>
[INFO]
[INFO] --- maven-enforcer-plugin:1.0:enforce (enforce-maven) @ uri-template ---
[INFO]
[INFO] <<< maven-source-plugin:2.2.1:jar (default) @ uri-template <<<
[INFO]
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ uri-template ---
[INFO] Building jar: /home/fge/src/perso/uri-template/target/uri-template-0.5-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
4

1 回答 1

0

好的,我找到了答案。

但它是丑陋的。就像在,巨魔看起来比那更好。所以,如果你知道更好的方法,我会全力以赴。

使用的是Maven antrun插件;我确保它在 jar 插件之后执行,并且我更新了 jar 以便将 META-INF/services/xxxx 文件添加到其中:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <links>
                    <link>http://docs.oracle.com/javase/6/docs/api</link>
                    <link>http://docs.guava-libraries.googlecode.com/git/javadoc</link>
                    <link>http://fge.github.io/msg-simple</link>
                    <link>http://jsr-305.googlecode.com/svn/trunk/javadoc</link>
                    <link>http://fge.github.com/btf</link>
                </links>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <version>2.3.7</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <property name="jarname"
                                value="${project.name}-${project.version}.jar"/>
                            <property name="victim"
                                value="${project.build.directory}/${jarname}"/>
                            <property name="serviceFile"
                                value="${project.build.directory}/classes/META-INF/services/${serviceName}"/>
                            <echo>${victim}</echo>
                            <echo>${serviceFile}</echo>
                            <jar destfile="${victim}" update="true">
                                <zipfileset file="${serviceFile}"
                                    prefix="META-INF/services/"/>
                            </jar>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-repository-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>bundle-create</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <id>sign-artifacts</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>sign</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>eu.somatik.serviceloader-maven-plugin</groupId>
            <artifactId>serviceloader-maven-plugin</artifactId>
            <version>1.0.2</version>
            <configuration>
                <services>
                    <param>${serviceName}</param>
                </services>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我尝试了各种东西,包括不起作用的 maven shade 插件,以及许多其他我不记得的东西......

于 2013-06-11T09:33:18.127 回答