简体   繁体   English

自动编译Bash脚本无法正常工作

[英]Auto-Compile Bash Script not working

Simply put, I want to manually compile Java files through the command line, but don't want to do it for each directory. 简单地说,我想通过命令行手动编译Java文件,但不想为每个目录执行此操作。 I tried to write this script but it doesn't seem to work. 我试着写这个脚本,但它似乎不起作用。

CUR=$PWD                              # save workspace directory to return
for line in `find -type d -name src`  # find and loop through src paths
do
    eval cd $line                     # go to src
    eval javac *.java
    eval jar *.class                  # I need to have a jar for my project
    eval mv ../bin/                   
    cd $CUR
done

I don't think you need to use eval at all in that script. 我认为你不需要在该脚本中使用eval

But one problem is probably that your source directories are not all called src (or are they?) What is your directory structure like? 但有一个问题可能是您的源目录并非都被称为src (或者它们是什么?)您的目录结构是什么样的?

Also you probably need to have a single jar for your project, not multiple jars, so that bit won't work either. 此外,您可能需要为您的项目使用一个jar,而不是多个jar,因此该位也无效。

You should probably look into using a real Java build tool such as Ant or Maven; 您应该考虑使用真正的Java构建工具,如Ant或Maven; they solved this problem over a decade ago. 十多年前他们解决了这个问题。

Some improvements to your existing script: 对现有脚本的一些改进:

for line in $(find -type d -name src)  # find and loop through src paths
do
    (
    cd $line                     # go to src
    javac *.java
    jar *.class                  # I need to have a jar for my project
    mv ../bin/                   
    )
done

Use parenthesis to execute the inner part in a subshell and avoid cd issues. 使用括号在子shell中执行内部部分并避免cd问题。 Also I see no reason for eval . 我也没有看到eval理由。

Are you sure all your java files are all directly in a folder called src without any package structure? 你确定你所有的java文件都直接在一个名为src的文件夹中而没有任何包结构吗? Are you sure they need no further classpath information to compile? 你确定他们不需要进一步的类路径信息来编译吗?

What exactly is the problem, that makes you think it does not work? 究竟是什么问题,让你觉得它不起作用?

Is there a good reason, why you are not using grade, maven, or ant to build your project? 有没有充分的理由,为什么你不使用等级,专家或者蚂蚁来构建你的项目?

(Feel free to simply update the question as a response to the questions.) (可以随意更新问题作为对问题的回答。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM