How to include source file(.java file) parallel to .class file in the generated jar using maven

烈酒焚心 提交于 2020-01-16 00:44:27

问题


I wanted to create my jars with wars with that should include source file(.java file) parallel to .class file in the generated jar using maven. (I know there are some plugins available to generate a separate xyy-sources.jar file. But I dont want to create a seperate source jar. I need a single jar file with both .class and .java file exists parallel)


回答1:


You only have to add resources under build tag. For example.

<build>
   <resources>
      <resource>
        <directory>src/main/java</directory>
      </resource>     
    </resources>
</build>

Now every jar and war file you creat would contain .java files also. :)




回答2:


I'm still not sure I understand why you would want to do this, but you could use the maven-resource-plugin and specifically its copy-resource goal to copy your java files to any location within your build directory. So for example to copy them to the classes folder which then gets included in the jar that is built automatically you could do the following:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>process-resources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.outputDirectory}/src-files-location</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/java</directory>
                        <includes>
                            <include>**/*.java</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>



回答3:


I am not sure if including .java adjacent to .class is a very good idea. Any specific reason you want to do that?

If you need to include sources, you can create a separate source jar (which is not you asked for, but is recommended way) like this by executing mvn package

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <executions>
        <execution>
          <id>attach-sources</id>
          <goals>
            <goal>jar</goal>
          </goals>
        </execution>
      </executions>
    </plugin>

Does this help? http://maven.apache.org/plugin-developers/cookbook/attach-source-javadoc-artifacts.html



来源:https://stackoverflow.com/questions/25779900/how-to-include-source-file-java-file-parallel-to-class-file-in-the-generated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!