15

For my Java project which uses Maven I have added Twitters Bootstrap lately to my repository on GitHub. Now I have noticed that my GitHub language statistic changed from

  • 100% Java

to

  • 66,% Java 33,3% JavaScript

Since I didn't write the JS part, is there a way to manage that this isn't counted as mine or how would one normally manage foreign JS code in a GitHub repository or projects in general

Mahoni
  • 7,088
  • 17
  • 58
  • 115

5 Answers5

17

I have faced that problem lately, too. There are several ways to deal with it:

  • use the performance optimized versions, for instance bootstrap.min.js instead of bootstrap.js etc. GitHub uses the file size to determine the language proportion of each repository. This won't reduce it to zero, but at least it reduces the proportion

  • This might be what you want: GitHub has a vendor list with regular expressions which are exluced from the language graph. There you will see, that jQuery is already part of the exclusion. There is also a pending pull request to add Twitter Bootstrap. If this does not get pass you can always add the files to a directory called vendor there they will be excluded from the statistics.

  • use the Maven AntRun Plugin: With it you could simply download the JavaScript library files on request with Ant Get

  • there are also Maven repositories for these JavaScript libraries: Bootstrap-Maven. However, the files are mounted at /ext/bootstrap/ and I have no idea how this helps

  • there are even more solutions, like Maven War Plugin using overlays.

Finally my advice: just put the files into your repository. I have looked into some bigger projects on GitHub using jQuery and Twitter Bootstrap and they all put their JavaScript library files into the repository.

The advantage of these JavaScript library files is that they don't have to be compiled what so ever. Use this advantage and don't add additional burdens for the sake of aesthetic. Best practice seems to be adding it to the repository and live with the stupid language graph. Who knows, maybe certain files will be ignored in the future.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
4

The question is fairly old, but for future reference, it should be noted that GitHub added in 2014 a new feature to handle this thing. Possibly thanks to an open issue on the linguist repository.

This feature allows regarding certain files as libraries, documentation or a specific language.

Just add .gitattributes file to your repository. For example, to ignore a library folder, just add the line:

library/* linguist-vendored

Or to set the docs folder, add the line:

docs/* linguist-documentation

More examples can be found here.

Liran Funaro
  • 2,750
  • 2
  • 22
  • 33
3

Two observations:

  • The GitHub statistics are not incorrect: your project consists of those amounts of Java and JavaScript code. It would be incorrect to say that it is a 100% Java project.
  • It's not common to include dependencies in your own code repository. You install them manually or automatically (using a system/method that varies for each programming language / environment). For example, Python has a file called requirements.txt which lists the dependencies but in Java I can imagine there to be a similar approach.
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • What do you say to Hames Allardices hint to **git submodule**? – Mahoni Jun 14 '12 at 19:57
  • They can be worth a look in your situation. I haven't used them before so I can't comment on that really. – Simeon Visser Jun 14 '12 at 20:02
  • I don't see how this helps in my situation. Python is not what I am using and the argument with the GitHub statistic seems weak. It wouldn't be any fun either if the statistic would include all the JAR libraries you are using or if every one of your project code statistic would include the Linux Kernel code, since you are using it, etc. – Mahoni Jun 27 '12 at 19:21
  • How do you currently install dependencies then? These should be installed as part of your build script, you don't have to include them in your repository. – Simeon Visser Jun 27 '12 at 19:27
  • With Apache Maven, but this is primarily for Java JARs – Mahoni Jun 27 '12 at 19:39
3

Use git submodules or put your 3rd party files in a directory called 'vendor'. Then Github will not count them as part of your code

Munter
  • 1,079
  • 5
  • 7
1

It is a Maven best practice to split a project into several submodules, each one with a small, well-defined purpose. If you're putting different kinds of sources in the same Maven module, you're not using it right. Assuming that in the end you'll end up with a .war, then my advice is to add Bootstrap, jQuery and whatever other libraries you're using as zip dependencies that will be included in the war build.

Let's assume that you can get these libraries available as downloadable Maven dependencies (I'll discuss below your options for accomplishing this), then you would just have to declare them as dependencies for your war artifact:

  <dependency>
    <groupId>com.twitter.bootstrap</groupId>
    <artifactId>twitter-bootstrap</artifactId>
    <version>2.0.4</version>
    <type>zip</type>
    <scope>runtime</scope>
  </dependency>

As far as your own code is concerned, that is all the Bootstrap that you need to "include".

How do you get that zip available? Well, the best thing would be to see it published on the central Maven repository, but that's not something that you should do; Twitter should step up and take care of all the Maven users out there.

The next best thing would be to find another public repository that already hosts it well packaged, but I couldn't find one with a nice packaging after a short Google search, maybe you'll have more luck.

The third option is to take things into your own hands, and be the one that creates the proper Maven build for that zip and publish it on your own repository.

One solution I prefer for building such resource zips is to use the maven assembly plugin, and you can use as an inspiration this maven project: put the sources (js, css and image files) in src/main/resources, and add a simple assembly file that just copies all the files from the resources directory into a zip. Set up a maven repository on a server (a simple HTTP serving machine with sftp access is enough), and do a maven release on that project to upload the artifact on the server.

(You should have a maven repository for releasing your own projects anyway)

Next, you would need to declare your repository in your real project's pom file, add a dependency on the bootstrap artifact, and configure the maven-dependency-plugin to unzip it in a location of your choice:

...
  <repositories><repository>
    <id>bootstrap-repo</id>
    <name>bootstrap public repository</name>
    <url>http://repo.yourhost.net/release</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository></repositories>
...
  <dependencies><dependency>
    <groupId>com.twitter.bootstrap</groupId>
    <artifactId>twitter-bootstrap</artifactId>
    <version>2.0.4</version>
    <type>zip</type>
    <scope>runtime</scope>
  </dependency></dependencies>
...
  <build><plugins><plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack-dependencies</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
        <configuration>
          <artifactItems>
            <!-- Unpack the Bootstrap library -->
            <artifactItem>
              <groupId>com.twitter</groupId>
              <artifactId>twitter-bootstrap</artifactId>
              <type>zip</type>
              <outputDirectory>${project.build.directory}/bootstrap</outputDirectory>
            </artifactItem>
          </artifactItems>
        </configuration>
      </execution>
    </executions>
  </plugin></plugins></build>
...

(I'm not sure it would be safe to use com.twitter as the groupId, since they're not the ones publishing that artifact, so use your own groupId)

Since your own code just declares a dependency on this twitter-bootstrap artifact, you can keep your main GitHub repository clean of third party code. You don't have to commit the project that builds the twitter-bootstrap artifact at all if you don't want your coding statistics to be influenced, although every line of code you write should be committed.

Alternative: maven-antrun-plugin

An alternative to using a mavenized packaging of third party libraries is to extract those files from their official zip distributions using ant tasks in the maven-antrun-plugin to download the zip and extract the files from it.

Or, you can use this trick in the twitter-bootstrap maven project in order to avoid copying their source files at all.

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62