V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xy2401
V2EX  ›  Java

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

  •  
  •   xy2401 · 2021-03-18 08:36:25 +08:00 · 2442 次点击
    这是一个创建于 1128 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    7 条回复    2021-03-18 14:25:01 +08:00
    xy2401
        1
    xy2401  
    OP
       2021-03-18 08:48:04 +08:00
    <build>
    <sourceDirectory>./</sourceDirectory>
    </build>

    重新试了一下 mvn 命令支持最后有打包出 class 。eclipse 不支持 run 这个类。idea 支持 run 。vscode 还没有尝试
    john6lq
        2
    john6lq  
       2021-03-18 08:48:57 +08:00 via iPhone
    xy2401
        3
    xy2401  
    OP
       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
        4
    xy2401  
    OP
       2021-03-18 08:55:54 +08:00
    @john6lq 看起来 maven 的 ide 不完全支持。gradle 我后面再试一下。
    borisz
        5
    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
        6
    loshine1992  
       2021-03-18 11:31:48 +08:00
    gradle 可以用 sourceSets 配置
    xiaokongwu
        7
    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 不确定了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5756 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:13 · PVG 10:13 · LAX 19:13 · JFK 22:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.