V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xy2401
V2EX  ›  Java

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

  •  
  •   xy2401 · Mar 18, 2021 · 3576 views
    This topic created in 1876 days ago, the information mentioned may be changed or developed.

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

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

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

    7 replies    2021-03-18 14:25:01 +08:00
    xy2401
        1
    xy2401  
    OP
       Mar 18, 2021
    <build>
    <sourceDirectory>./</sourceDirectory>
    </build>

    重新试了一下 mvn 命令支持最后有打包出 class 。eclipse 不支持 run 这个类。idea 支持 run 。vscode 还没有尝试
    xy2401
        3
    xy2401  
    OP
       Mar 18, 2021
    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
       Mar 18, 2021
    @john6lq 看起来 maven 的 ide 不完全支持。gradle 我后面再试一下。
    borisz
        5
    borisz  
       Mar 18, 2021
    <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  
       Mar 18, 2021
    gradle 可以用 sourceSets 配置
    xiaokongwu
        7
    xiaokongwu  
       Mar 18, 2021
    <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 不确定了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3484 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 11:44 · PVG 19:44 · LAX 04:44 · JFK 07:44
    ♥ Do have faith in what you're doing.