问题
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