插件生成的文件不在最终的jar中:为什么?

时间:2013-06-10 22:22:06

标签: maven maven-3

环境:Ubuntu 64bit,maven 3.0.4。

编辑:我找到了办法,见下面的答案;但与我提出的相比,猴子修补是优秀设计的缩影...

我使用this plugin查找用于JDK ServiceLoader的接口的实现。

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

我已经尝试过google了很多次,打破了我的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] ------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案。

但它是UGLY。比如,一个巨魔看起来比那更好。所以,如果你知道一个更好的方法,我会全力以赴。

使用的是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插件,以及其他许多我不记得的东西......