Java 的 maven/gradle 如何可以不使用../src/main/ Java 源码目录,而指定根目录

2021-03-18 08:36:25 +08:00
 xy2401

我之前搜索过很多次好像配置都不怎么成功。 我期望的效果目录下面就一个 pom.xml 管理依赖,App.java 运行代码

--project --App.java --pom.xml

因为不是一整个工程有时候就是一个简单代码可能就一个类不想有这么多层级目录

2457 次点击
所在节点    Java
7 条回复
xy2401
2021-03-18 08:48:04 +08:00
<build>
<sourceDirectory>./</sourceDirectory>
</build>

重新试了一下 mvn 命令支持最后有打包出 class 。eclipse 不支持 run 这个类。idea 支持 run 。vscode 还没有尝试
john6lq
2021-03-18 08:48:57 +08:00
xy2401
2021-03-18 08:52:17 +08:00
vscode 也不支持

The file App.java isn't on the classpath, the runtime may throw class not found error. Do you want to add the parent folder " " to Java source path?

Unsupported operation. Please use pom.xml file to manage the source directories of maven project.
xy2401
2021-03-18 08:55:54 +08:00
@john6lq 看起来 maven 的 ide 不完全支持。gradle 我后面再试一下。
borisz
2021-03-18 11:13:05 +08:00
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../../../../folder1/xxx</source>
<source>${basedir}/../../../../folder2/xxx2</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
loshine1992
2021-03-18 11:31:48 +08:00
gradle 可以用 sourceSets 配置
xiaokongwu
2021-03-18 14:25:01 +08:00
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>

maven 里这是 pom 默认的配置,在<build>标签下,你可以改成你的目录,idea 会识别这个配置,其他 ide 不确定了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/762660

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX